|
 |
|
Dev Articles Community Forums
> Programming
> C/C++ Help
|
Program is not breaking out of loop. Please help C++ Lab
Discuss Program is not breaking out of loop. Please help C++ Lab in the C/C++ Help forum on Dev Articles. Program is not breaking out of loop. Please help C++ Lab C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.
|
|
 |
|
|
|
|

Dev Articles Community Forums Sponsor:
|
|
|

December 1st, 2012, 06:28 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 6 m 23 sec
Reputation Power: 0
|
|
|
Program is not breaking out of loop. Please help C++ Lab
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.
Code:
#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;
}
I'm thinking my problem has to do with this block of code
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.
Code:
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);
}
|

December 1st, 2012, 06:54 PM
|
|
Contributing User
|
|
Join Date: Jan 2012
Posts: 101
Time spent in forums: 22 h 13 m 51 sec
Reputation Power: 2
|
|
First of all I wanna say this.
Using breaks in loops and such is not a good solution.
I strongly recommend using bools or other conditions in while loops to end it.
Switch/case is the only one the use of break is ok.
Code:
#include <deque>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
int id;
float gpa;
char gender;
}; // Student
//Function declarations here
void printStudents(Student&);
int main()
{
// create an empty list
deque<Student> student;
// declare variables
Student aStudent;
char answer;
int count = 0;
bool quit = false;
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 (!quit)
{
// 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);
if(student.size() == count)
quit = true;
}
// display the inputted records
cout << endl;
for (int i = 0; i < student.size(); i++)
{
printStudents(student[i]);
}
}
return 0;
}
//Function defintions here
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
|

December 2nd, 2012, 03:16 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 2
Time spent in forums: 6 m 23 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by ediz First of all I wanna say this.
Using breaks in loops and such is not a good solution.
I strongly recommend using bools or other conditions in while loops to end it.
Switch/case is the only one the use of break is ok.
Code:
#include <deque>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
int id;
float gpa;
char gender;
}; // Student
//Function declarations here
void printStudents(Student&);
int main()
{
// create an empty list
deque<Student> student;
// declare variables
Student aStudent;
char answer;
int count = 0;
bool quit = false;
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 (!quit)
{
// 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);
if(student.size() == count)
quit = true;
}
// display the inputted records
cout << endl;
for (int i = 0; i < student.size(); i++)
{
printStudents(student[i]);
}
}
return 0;
}
//Function defintions here
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
|
Thanks for the tip. I've been working on this for awhile. Would you be able give an example on how to incorporate this into my program?
|

December 2nd, 2012, 10:10 AM
|
|
Contributing User
|
|
Join Date: Jan 2012
Posts: 101
Time spent in forums: 22 h 13 m 51 sec
Reputation Power: 2
|
|
Code:
#include <deque>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
int id;
float gpa;
char gender;
void print()
{
cout << "Name = " << left << setw(20) << name;
cout.fill('0');
cout << " ID = " << right << setw(7) << id
<< ", GPA = " << gpa;
cout << " Gender = " << gender << endl;
cout.fill(' ');
}
}; // Student
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 (student.size() < count)
{
// 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);
bool quitGPALoop = false;
while (!quitGPALoop)
{
cout << "Enter the GPA: ";
cin >> aStudent.gpa;
cin.ignore(1000, 10);
if (aStudent.gpa >= 0 && aStudent.gpa <= 5)
quitGPALoop = true;
else
cout << "GPA must be between 0.0 and 5.0" << endl;
}
bool quitGenderLoop = false;
while (!quitGenderLoop)
{
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')
quitGenderLoop = true;
else
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]);
student[i].print();
}
}
system("PAUSE");
return 0;
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|