Why can't I display all the nodes from myList

When I run the program only the last node is displayed
1
2
Enter a list of integers ending with -999.
2 15 8 24 34 -999

When I run the program I get:
1
2
3
34
Process returned 0 (0x0)   execution time : 125.461 s
Press any key to continue.

here is the whole program:
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
  #include<iostream>
#include<cassert>
using namespace std;

struct nodeType
{
    int info;
    nodeType *link;
};
nodeType *buildListForward()
{
    nodeType *first, *newNode, *last;
    int num;
    cout<<"Enter a list of integers ending with -999.\n";
    cin >> num;
    first = NULL;
    while(num != -999)
    {
        newNode = new nodeType;
        assert(newNode != NULL);

        newNode->info = num;
        newNode->link = NULL;

        if(first == NULL)
        {
            first = newNode;
            last = newNode;
        }
        else
        {
            last->link = newNode;
            last = newNode;
        }
        cin >> num;
    } // end while
    return first;
    } // end buildListForward
class linkedList
{
private:
    nodeType *first,*last;
public:
    linkedList()
    {
        first = NULL;
        last = NULL;
    }
};

void display() {
   struct nodeType* ptr, *first;
   ptr = first;
   while (ptr != NULL) {
      cout<< ptr->info <<" ";
      ptr = ptr->link;
   }
}
 int main()
 {
     linkedList myList;
    buildListForward();
     display();
 }
 In function 'void display()':
53:15: warning: 'first' is used uninitialized in this function [-Wuninitialized]


That is not the only problem in your program.
What is the purpose of 'myList'? You don't use it.
Why does the 'buildListForward' return a pointer? You don't use that value.
I get an error when I want to use:
 
    myList.buildListForward();


Here is the error:
1
2
3
4
5
6
7
8
In function 'int main()':
C:\Martin\MalikChapter5\buildListForwardRevision.cpp:63:12: error: 'class linkedList' has no member named 'buildListForward'
     myList.buildListForward();
            ^
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 


bopaki wrote:
error: 'class linkedList' has no member named 'buildListForward'


The code you have given (at the moment) doesn't have a member function called 'buildListForward' ... although there is a non-member function of that name.

Need to see your actual code eliciting that error, please.
Its all sorted out now....
Thank you all!!!
Topic archived. No new replies allowed.