Program is not breaking out of loop. Please help

Hi everyone. I'm working on a program but I'm stuck on this one part that I cannot figure out. In this lab we are supposed to prompt the user for how many times they want to enter in records and after that number the program should quit. However my program seems to be stuck in a loop and never quits even after the number entered has been exceeded. I've been looking through my code and cannot figure out what I've done wrong. If anyone could help me out I'd greatly appreciate it.

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
102
103
#include <deque>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

struct Student
{
  string name;
  int id;
  float gpa;
  char gender;
}; // Student

void printStudents(Student& student)
{
  cout << "Name = " << left << setw(20) << student.name;
  cout.fill('0'); 
  cout << " ID = " << right << setw(7) << student.id
    << ", GPA = " << student.gpa;
  cout << " Gender = " << student.gender << endl;
  cout.fill(' '); 
} // printStudents


int main()
{
  // create an empty list
  deque<Student> student;
  
  // declare variables
  Student aStudent;
  char answer;
  
  int count = 0;

  cout << "How many records? ";
  cin >> count;
  cin.ignore(1000,10);

  if (count == 0) 
  {
    cout << endl;
  } 
  else 
  {
    cout << endl;

  // prompt user to enter student records
  cout << "To quit, leave blank and press enter." << endl;
  while (true) 
  {
    // prompt user for input
    cout << "Enter the name [Last, First]: ";
    getline(cin, aStudent.name);
    
    // check for quit
    if (aStudent.name.length() == 0) break;
    
    cout << "Enter the Student ID: ";
    cin >> aStudent.id;
    cin.ignore(1000, 10);
    
    while (true) 
	{
      cout << "Enter the GPA: ";
      cin >> aStudent.gpa;
      cin.ignore(1000, 10);
      if (aStudent.gpa >= 0 && aStudent.gpa <= 5) break;
      cout << "GPA must be between 0.0 and 5.0" << endl;
    }
    
    while (true) 
	{
      cout << "Enter the gender [M/F]: ";
      cin >> aStudent.gender;
      cin.ignore(1000, 10);
      aStudent.gender = toupper(aStudent.gender);
      if (aStudent.gender == 'M' || aStudent.gender == 'F') break;
      cout << "Gender must be M or F." << endl;
    }
    
    cout << endl;
    
    // add record to list
    student.push_back(aStudent);
    

  }
  
  // display the inputted records
  cout << endl;
  for (int i = 0; i < student.size(); i++) 
  {
    printStudents(student[i]);
  }

  }
  
  return 0;
}

Last edited on
You never use count inside the loop. Your loop breaks when the user enters an empty name.
Is there anyway of getting rid of the count inside the loop but keep the continuous prompt that I've got right now? Everything I've tried keeps causing my code not to compile properly.
If anyone could help me out I would greatly appreciate it. I've been stuck on this for hours :(
All you've got to do is change your loop condition (you know- the one on line 51) so that you stop getting input after count iterations.
So I got rid of the break with 0 is entered that was on line 58. Everything besides that looks correct to me though. I would greatly appreciate it if someone could give me a hint on how to break the input after count iterations.

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
while (true) 
  {
    // prompt user for input
    cout << "Enter the name [Last, First]: ";
    getline(cin, aStudent.name);
       
    cout << "Enter the Student ID: ";
    cin >> aStudent.id;
    cin.ignore(1000, 10);
    
    while (true) 
	{
      cout << "Enter the GPA: ";
      cin >> aStudent.gpa;
      cin.ignore(1000, 10);
      if (aStudent.gpa >= 0 && aStudent.gpa <= 5) break;
      cout << "GPA must be between 0.0 and 5.0" << endl;
    }
    
    while (true) 
	{
      cout << "Enter the gender [M/F]: ";
      cin >> aStudent.gender;
      cin.ignore(1000, 10);
      aStudent.gender = toupper(aStudent.gender);
      if (aStudent.gender == 'M' || aStudent.gender == 'F') break;
      cout << "Gender must be M or F." << endl;
    }
    
    cout << endl;
    
    // add record to list
    student.push_back(aStudent);
   
  }
Topic archived. No new replies allowed.