MHB Fixing Code to Print "Print": Nodes and Pointers Programming Error

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Nodes Pointers
AI Thread Summary
The discussion centers on a programming issue where the user is unable to print output from their linked list implementation in C++. The primary problem arises from errors in the `Print` method and the main function. The `Print` method incorrectly declares a variable `c` of type `Print`, which is undefined, leading to a compilation error. Additionally, the main function attempts to call `l.head(c)`, which is incorrect as `head` is not a function and `c` is not declared. The solution involves correcting the `Print` method to properly iterate through the linked list and print the keys stored in each node. The user acknowledges the feedback and recognizes the source of the errors.
needOfHelpCMath
Messages
70
Reaction score
0
what is wrong with my programming D: won't print out the "Print"
Code:
 #include <iostream>
#include <cstdlib>
#include<string>

using namespace std;

class Node {   
public:
   Node();
   Node* prev;
   string key;
   Node* next;
   
};

Node::Node() {
   prev = 0;
   next = 0;
   
   return;
}
   

class LinkedList {
public:
  LinkedList();
  void Insert(string key);
  void Print();
  Node* Find(string key);
  void Delete(Node* x);

  Node* head;
};

LinkedList::LinkedList() {
   head = 0;

   
   return;
}
 
 void LinkedList::Insert(string key) {
    Node* n;
    n = new Node;
      n->key = key;
     
      
      // Link new node to current head
      // of list
      n->next = head;
     
      // Make head point to new node
      head = n;
      return;
   }
   
   void LinkedList::Print() {
      Print* c;
      
      return;
   }int main () { 
  LinkedList l;
  Node* n = 0;
  string line;
   
  
    while (true){

     getline(cin, line);
     if (line.empty()){
        break;
     }

     l.Insert(line);
  }

   n = l.head(c);
   
   while (n!=0){
      cout << n->key << endl;
      n = n->next;
   }
   return 0;
}

I want to print out "Print" with the arrow

Code Runs:

Code:
main.cpp: In member function 'void LinkedList::Print()':
main.cpp:58:14: error: 'c' was not declared in this scope
       Print* c;
              ^
main.cpp: In function 'int main()':
main.cpp:80:15: error: 'c' was not declared in this scope
    n = l.head(c);
               ^
main.cpp:80:16: error: expression cannot be used as a function
    n = l.head(c);
^
 
Technology news on Phys.org
The first error message essentially tells you the problem: In "LinkedList.Print()" you have "Print* c" but I see no other mention of "c". Where is "c" defined?
 
HallsofIvy said:
The first error message essentially tells you the problem: In "LinkedList.Print()" you have "Print* c" but I see no other mention of "c". Where is "c" defined?

ahhhh okay i see thank you!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
5
Views
2K
Replies
9
Views
3K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
1
Views
3K
Back
Top