This version of the program freezes & does nothing after entering the data

Nothing happens after entering the data
1
2
3
Enter numbers ending with -999
45 62 34 98 123 -999


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
71
  #include<iostream>
#include<list>
#include<cassert>
using namespace std;

struct Node
{
int data;
Node *link;
};
typedef Node *NodePtr, *head, *tmp;
class linkedList
{
public:
       void head_insert(NodePtr& head, int the_number);
       //Precondition: The pointer variable head points to
       //the head of a linked list.
       //Postcondition: A new node containing the_number
       //has been added at the head of the linked list.
       bool empty() const;
       void print();
};
void linkedList::head_insert(NodePtr& head, int the_number)
 {
 NodePtr temp_ptr;
 temp_ptr = new Node;

 temp_ptr->data = the_number;

 temp_ptr->link = head;
 head = temp_ptr;
 }

//void linkedList::print()
//{
//Display the nodes
//NodePtr tmp;
//     tmp = head;
  //        while(tmp)
    //      {
//cout << tmp->data << " " <<endl;
  //   tmp = tmp->link;
//}
//}
//Function Definition

 int main()
{
    linkedList list;
    NodePtr head, tmp, first;
    int num;
    cout<<"Enter numbers ending with -999\n";
    cin >> num;
    while(num != -999)
    {
        list.head_insert(head, num);
    }
    cin >> num;
    
//Display the nodes
//NodePtr tmp;
     tmp = head;
          while(tmp)
          {
cout << tmp->data << " " <<endl;
     tmp = tmp->link;
}
    return 0;
}
   
I guess you want to move line 58 inside the loop. After line 56.
Thanks coder777!!! It runs fine now.

1
2
3
Enter numbers ending with -999
45 78 98 12 34 1732 -999
1732 34 12 98 78 45


Except that after a few minutes I get an error report screen in the middle of the screen. This message has an option to send it to Microsoft to report the problem

Thanks very much!!!!
The problem is that on line 50 you leave head uninitialized. Therefore you have no nullptr to mark the end of the list. Change:

NodePtr head = nullptr, tmp, first;

There should be a warning according to the head problem.

It is better to not ignore warnings.
Thank you coder777
The problem have now been solved
Topic archived. No new replies allowed.