Infinite Loop - Link Lists

My print_to_Screen method is causing an infinite loop and I don't like why. Anyone help me? I'm baffled.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

struct person_info
{
     string firstname;
     double height;  // height in inches
     double weight;     // weight in pounds
     int ID;
};       

typedef person_info infoType;

struct nodeType
{
     infoType info;
     nodeType *link;
};

person_info newOne (ifstream& inFile);
void print_to_Screen(nodeType *list);

int main(){
    
    ifstream inFile;
    
    nodeType *first, *temp;
    person_info one;
    
    inFile.open("peeps.txt");
    
    if (!inFile )
    {
         cout << " file not found ";
         system ( "pause");
         return 1;
    }
    
    while(!inFile.eof())
    {
        one = newOne(inFile);
        temp = new nodeType;
        temp->info = one;
        temp->link = first;
        first = temp;
    }
    print_to_Screen(first);
    
    inFile.close();
 
    system("pause");
    return 0;
}



person_info newOne (ifstream& inFile) 
{  
    string name;
    double weight;
    double height;
    int ID;
      person_info temp;

   
  	    inFile >> name;	
  		inFile >> ID;
  		inFile >> height;  
        inFile >> weight;

    temp.firstname = name;
    temp.weight = weight;
    temp.height = height;
    temp.ID = ID;
       
    return temp;
}

void print_to_Screen(nodeType *list)
{  
      
      nodeType *current;
      current = list;
      

     while (current != NULL)
     {
        cout << setw(10) << (current->info.firstname);
        cout << setw(7) << (current->info.ID);
        cout << setw(7) << (current->info.weight);
        cout << setw(7) << (current->info.height);
        
        current = current->link;
        cout << endl;
     }
     cout << endl;
}
Last edited on
Because there is no NULL in your linked list,and your while loop in print_to_Screen function looks for NULL. For that you have to change line no.31 to nodeType *first=NULL, *temp;
Last edited on
Haha. I feel dumb. THANKS!!
Topic archived. No new replies allowed.