Linked list and infinite loops

For a piece of code I'm writing for a class, I keep getting an infinite loop with the console output of the program during execution as soon as I enter the sentinel value to end the input process. Any pointers on what I did wrong would be appreciated.

Here's the code in question:

#include<iomanip>
#include<iostream>
#include<string>
using namespace std;

struct Student {

string name;
int id;
float gpa;
Student* next;

};


int main() {

Student* head = 0;
string rRequest = "Yes";
int count = 0;

while(rRequest != "No") {
Student* aStudent = new Student;

cout << "Do you want to enter a student record?[Yes/No]: ";
getline(cin, rRequest);

if (rRequest == "No") break;

cout << "Student name: " ;
getline(cin, aStudent->name);

cout << "Student ID: ";
cin >> aStudent->id;
cin.ignore(1000, 10);

cout << "Student gpa: ";
cin >> aStudent->gpa;
cin.ignore(1000, 10);


aStudent->next = head;
head = aStudent;
count += 1;




} // while




Student* i;
for (i = head; i ; i->next)
{
cout << "Name = " << left << setw(30) << i-> name ;
cout.fill('0');
cout << "ID = " << right << setw(7)
<< i->id << ", gpa = "
<< i->gpa << endl;
cout.fill(' ');
} // for


while(head) {

Student* next = head->next;
delete head;
head = next;

}

return 0;

}
This is dodgy..

 
for (i = head; i ; i->next)


It should probably be:

 
for (i = head; i ; i = i->next)
Galik, thank you so much. I can't believe I overlooked that. *smashes face in keyboard out of shame*
Last edited on
We all do it. If you stare at a problem too long you can get code blindness. You start seeing what you think you wrote rather than what you actually did write. ;o)
Topic archived. No new replies allowed.