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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

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