My function : void print() does not print out the list

Everything look fine but the last part which should print the list does not

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
#include<iostream>
#include<cassert>
using namespace std ;
   template<class Type>
   struct nodeType
{
    int info;
  nodeType *link;
    };
class buildForward {
public:
    nodeType<int> *buildListForward()   

        {
        nodeType<int> *first, *newNode, *last;
        int num;
    
    cout<<"Enter numbers ending with-999"<<endl;
     cin >> num;
        first = NULL;
        last = NULL;
       while(num != -999)
    {
        newNode = new nodeType<int>;
       
        newNode->info = num;
        newNode->link = NULL;
        
        if(first == NULL)
        {
            first = newNode;
            last = newNode;
            
        }
        else
        {
             last->link = newNode;
            last = newNode;
                        
        }
        cin>> num;
} 
        return first;             
}
//    void print();
void print()
{
        nodeType<int> *current, *first = NULL;
        current = first;
        while(current != NULL)
        {
            cout<<current->info <<" ";
            current = current->link;
    }
        
    }
    };
    
    int main()
    {
        buildForward myList;
       myList.buildListForward()     ;
    myList.print();
return 0;
      }
 
  
Due to line 48 there is no way that it actually can print anything:

*first = NULL

You need to provide the return value of buildListForward() as a parameter of print:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void print(nodeType<int> *first)
{
        nodeType<int> *current = first;
...
}    

...

    int main()
    {
        buildForward myList;
    myList.print(myList.buildListForward());
return 0;
      }


Alternatively you first may be a member of class buildForward. Then the class would make sense.
Thanks coder777 it worked like a bomb.
I just wonder how come some people are so good with OOPs , whilest others battle like I do
Topic archived. No new replies allowed.