3 Replies - 11372 Views - Last Post: 28 March 2007 - 06:45 AM Rate Topic: -----

#1 kinkay716   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-March 07

Grade Calculator

Posted 27 March 2007 - 08:49 PM

Hello everyone out there,

I just started taking my first programming course and am having a bit of difficult with a problem and I hope someone can help.

My problem:

Create a program that asks for grades, A through D and display the result, If value is F then display "bummer" and display the value. repeat until user enters x.

If the user enters a invalid grade, display that is not a valid entry and loop back to the beginning.

When user enters 'x', confirm user would like to exit, if Y then terminate program, if N then loop back to beginning of the program. If an entry other than n or y is made, loop back to confirm exit.

here is my code:

#include <iostream.h>
#include <stdlib.h>

int main()
{
	  char grade;
	  char stopcheck;



		   cout<<"Enter a valid grade or x to stop: ";
		   cin>>grade;

		 //Begin Outer Do Loop 1
		 do
		 {
		   while (grade!='x')
		   {//Begin While for grade check
		   if (grade == 'a' || grade =='A')
			  {
			  cout<<"Your grade is: A"<<endl;
			  break;
			  }
		   if (grade == 'b' || grade =='B')
			  {
			  cout<<"Your grade is: B"<<endl;
			  break;
			  }
		   if (grade == 'c' || grade =='C')
			  {
			  cout<<"Your grade is: C"<<endl;
			  break;
			  }
		   if (grade == 'd' || grade =='D')
			  {
			  cout<<"Your grade is: D"<<endl;
			  break;
			  }
		   if (grade == 'F' || grade =='f')
			  {
			   cout <<"Bummer.\n";
			   cout <<"Your grade is: F"<<endl;
			   break;
			   }
		   else
			   {
			   cout<<"That is an invalid entry.\n";
			   break;
			   }

		   }//End while for grade check


		   //On break; statements from the while loop above should break to here.

		   //If grade is x
		   if (grade == 'x')
		   {


			  while (stopcheck !='y' || stopcheck !='Y' || stopcheck !='n' || stopcheck !='N')
			  {//Begin While stopcheck

				 cout<<"Are you sure you want to quit? (Y/N): ";
				 cin>>stopcheck;

				 if(stopcheck =='y' || stopcheck =='Y')
				 {
				 cout<<"Terminating Program";
				 break;
				 }

				 else if(stopcheck =='n' || stopcheck =='N')
				 {
				 cout<<"Enter a valid grade: ";
				 cin>>grade;
				 break;
				 }

				 else
				 {
				 cout<<"Invalid Input.\n";
				 }

			   } //End While Stopcheck
			}//End If grade is x

		  //break; from While stopcheck"Enter a valid grade: " should break to here
		  //It then should loop back to the beginning of do loop.
		} while (grade !='x'); //End Outer Do Loop 1

		//break; from While stopcheck "Terminating Program" should break to here
		return 0;

}





As it stands, it goes into a endless loop after I enter a grade... I've spent 8 hours on this already...Help?

Is This A Good Question/Topic? 0
  • +

Replies To: Grade Calculator

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Grade Calculator

Posted 27 March 2007 - 09:25 PM

Why the infinite loop? Well the program runs as follows:

 char grade;
	  char stopcheck;



		   cout<<"Enter a valid grade or x to stop: ";
		   cin>>grade; //Here I enter A...

		 //Begin Outer Do Loop 1
		 do
		 {
		   while (grade!='x')
		   {//Begin While for grade check
		   if (grade == 'a' || grade =='A')
			  {
			  cout<<"Your grade is: A"<<endl;
			  break;
			  //GOOD so far...
/* Now the break causes it to jump down to: */
		 if (grade == 'x') //note that grade=="A"
/*So it now jumpts down to: */
		} while (grade !='x'); //End Outer Do Loop 1
...and jumps WAY back up to the top... 
		 do
		 {
		   while (grade!='x')
		   {//Begin While for grade check
		   if (grade == 'a' || grade =='A')
			  {
			  cout<<"Your grade is: A"<<endl;
			  break;
/* and her have hit and endless loop............ */

solutions... rethink your loops... move the input to *inside* the loop so the user has a chance to change thier mind.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Grade Calculator

Posted 27 March 2007 - 09:36 PM

My advice is to avoid all this if() break; stuff and try to use the control structure as they are intended to be used.
here is an example:
	char input;
	do
	{
		
		//Do the inner loop till user gets tired...
		do
		{
			cout << "Enter a grade (or press X to exit):";
			cin >>input;
			if (input=='a'||input=='A')
			{
				cout << "Grade is: A\n";
			} else if (input=='b'||input=='B')
			{
				cout << "Grade is: B\n";
			} /*else if (...)
			{
				...
			}*/ else
			{
				cout << "Invalid entry!\n";
			}
		} while (!(input=='x'||input=='X'));

		do
		{
			cout <<"Are you sure you want to quit my wonderful program? (Y/N): ";
			cin >> input;
			if (input == 'y'||input=='Y')
			{
				cout << "You are the weekest link! Goodbye!\n";
				input='Y';
			} else if (input == 'n'||input=='N')
			{
				cout << "Good choice!\n";
				input='N';
			} else
			{
			   cout << "I SAID Y or N!\n";
			}
		} while (!(input=='Y'||input=='N'));
	} while (input!='Y');

Was This Post Helpful? 0
  • +
  • -

#4 kinkay716   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-March 07

Re: Grade Calculator

Posted 28 March 2007 - 06:45 AM

Thank you NickDMax, you have explained to me exactly what I was looking for. Thank you! I find it easy to get lost in this loops. Im sure I'll be asking for help again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1