C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2012, 07:28 PM
motegi99 motegi99 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 motegi99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
   
  }

Reply With Quote
  #2  
Old December 1st, 2012, 07:54 PM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 3
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

Reply With Quote
  #3  
Old December 2nd, 2012, 04:16 AM
motegi99 motegi99 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 motegi99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old December 2nd, 2012, 11:10 AM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 3
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;
}


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Program is not breaking out of loop. Please help C++ Lab


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap