My list does not print out the info

Sep 10, 2019 at 10:08am
Here is my list 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
66
  #include<iostream>
#include<list>
using namespace std;
struct nodeType
{
       int info;
       nodeType *link;
};
class buildLForward
{
public:
       
nodeType *buildListForward();
void print() const;
};
nodeType *buildLForward::buildListForward()
{
         nodeType *first, *last, *newNode;
         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 = NULL;
                            last = NULL;
                   }
                   else
                   {
                       last->link = newNode;
                       last = newNode;
                   }
                   cin >> num;
        } //end while
        return first;
} // end buildListForward
void buildLForward::print() const
{
nodeType *current, *first; //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;
}
}//end print

int main()
{
    int num;
    buildLForward list;
    nodeType *last;
    list.buildListForward();
    
      list.print();
       return 0;
       }
             
                          

When I try to print the list I get nothing
1
2
3
4
5
C:\Dev-Cpp>buildListForward
Enter a list of integers ending with -999.
45 56 89 -999

C:\Dev-Cpp>
Sep 10, 2019 at 10:36am
1
2
3
4
5
                   if(first == NULL)
                   {
                            first = NULL;
                            last = NULL;
                   }

Now that's a weird thing to write.

Probably you meant
1
2
3
4
5
                   if(first == NULL)
                   {
                            first = newNode;
                            last = newNode;
                   }




Please:
- correct as above

- remove the line with assert in

- define
nodeType *first, *last;
in your class declaration and NOT in either buildListForward() or print(). (TBH they would be better initialised to NULL (or, better, nullptr) in the class declaration.)
Last edited on Sep 10, 2019 at 10:49am
Sep 10, 2019 at 10:58am
I have done the changes as lastchance said. Now The program only print the last node only.
1
2
3
4
5
6

C:\Dev-Cpp>buildlistForward
Enter a list of integers ending with -999.
45 67 89 65 -999
65
C:\Dev-Cpp>

Sep 10, 2019 at 11:09am
Show your whole revised code. I get
Enter a list of integers ending with -999.
45 67 89 65 -999
45 67 89 65  



Make sure that you followed the instruction
define
nodeType *first, *last;
in your class declaration and NOT in either buildListForward() or print()
Last edited on Sep 10, 2019 at 11:11am
Sep 12, 2019 at 7:34am
Here is my code as requested by lastchance:
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
#include<iostream>
#include<list>
#include<cassert>
using namespace std;
struct nodeType
{
       int info;
       nodeType *link;
};
class buildLForward
{
public:
       
nodeType *buildListForward();
nodeType *first, *last;
void print() const;
};
nodeType *buildLForward::buildListForward()
{
         nodeType *first, *last, *newNode;
         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
void buildLForward::print() const
{
nodeType *current, *first; //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;
}
}//end print

int main()
{
    int num;
    buildLForward list;
    nodeType *last;
    list.buildListForward();
    
      list.print();
       return 0;
       }

                       
                      

Here is what I get:
1
2
3
4
5
C:\Dev-Cpp>buildlistforward
Enter a list of integers ending with -999.
45 67 98 12 -999
12
C:\Dev-Cpp>
Sep 12, 2019 at 8:15am
@Bopaki,
Please, READ MY LAST POST!

You redeclare first and last in buildLForward::buildListForward() (see line 20) - DON'T!

You redeclare first in vbuildLForward::print() (see line 47) - DON'T!

These would create completely new variables in those routines that just happen to have the same name as (and hence hide) the originals.

Don't you think it would cause utter confusion if I decided to call myself Bopaki?
Last edited on Sep 12, 2019 at 8:25am
Sep 12, 2019 at 8:27am
Thanks lastchance I corrected those errors, and now my program runs fine.
Topic archived. No new replies allowed.