Why is this program not printing out the list

When I try to print the program I get nothing
1
2
Enter a list of integers ending with -999.
12 45 76 89 -999

When I press "enter" to print the list
I get this:
1
2
3

Process returned 0 (0x0)   execution time : 89.943 s
Press any key to continue.

Nothing printed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include<iostream>
#include<cassert>
using namespace std;

struct nodeType
{
    int info;
    nodeType *link;
};
class linkedList
{
	private:
     	nodeType *first;
    	nodeType *last;
    	int count;
	public:
       linkedList();
 void print();
 void display();

void buildListBackward();
};
linkedList::linkedList()
{
       first = NULL;
       count = 0;
}
void linkedList :: buildListBackward()
{
    nodeType *first, *newNode;
	int num;
	cout << "Enter a list of integers ending with -999. "<< endl;
	cin >> num;
	first = NULL;
	while (num != -999)
	{
		newNode = new nodeType;  //create a node
		assert (newNode != NULL);
		newNode->info = num;
		newNode->link = first;
		first = newNode;
		cin >> num;
	}

}
void linkedList::print()
{
	nodeType * current; //pointer to traverse the list
	current = first; //set current point to the first node
	while (current != NULL) //while more data to print
	{
	cout << current->info << " ";
	current = current->link;
	}
}
void linkedList::display() {
    nodeType* ptr, *first;
   ptr = first;
   while (ptr != NULL) {
      cout<< ptr->info <<" ";
      ptr = ptr->link;
   }
}
 int main()
 {
     linkedList myList;
    myList.buildListBackward();
    // myList.display();
    myList.print();
 }
The print() is nothing because the myList.first is NULL.
(Btw, you should use nullptr; it is better than the NULL. )


Why is myList.first NULL?
You set it in the constructor and never change it since.
I got a strange error message after posting the first thread, hence I posted again
On line 30 you shadow first, hence the member variable first will not be changed. So remove the local variable first.
Thank you very much coder777
It worked first class after rempving *first in line 30

who would I be without you....
Topic archived. No new replies allowed.