Repeating a switch in case of a wrong input

interested in proper exception handling procedure.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3741 Views - Last Post: 20 May 2010 - 05:39 PM Rate Topic: ***** 1 Votes

#1 dhillon5573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-April 10

Repeating a switch in case of a wrong input

Posted 16 May 2010 - 07:50 PM

Dear Coders,

Ive been at this assignment for almost a month now and Im almost finished thanks to the creative input given to me by the kind people in this forum and more. Thank you very very much for that. Am at the final stage of my project where Id like to add exception handling procedures to my user interface which is something very new to me. My problem is the following. I have switch statement as below and included a try throw catch mechanism to detect invalid input. What I do not understand is how I can go about repeating the switch procedure if the user has messed up his input using proper exception handling procedures inbuilt in c++. I have a feeling there must be something i could say in the catch to get it to try again until the user succeds.hahhaha. Have a great day


try {

	char SchoolBook;
	cin>>SchoolBook;

	cout<<"\nYou have selected:\n"<<SchoolBook<<endl;

	if(SchoolBook != '1' && SchoolBook != '2' &&
		   SchoolBook != '3')
			throw SchoolBook;
		
		
	switch(SchoolBook){

		case'1':cout<<"Welcome to the database for the School of Physics and Astronomy"<<endl;

				cout<<"\n\nStudent"		
					<<"\nDetails"<<"\t\tPHYS 30101 "<<" PHYS 30151 "<<" PHYS 30222 "<<" PHYS 30342 "<<" PHYS 30471 "
					<<endl;
			
				for(int Student=0;Student<nStudents;Student++){

					if(Data[Student].getSchool()=="School of Physics and Astronomy"){

							Data[Student].printPhysicsBook();

					}
				}
																							break;

		case '2':cout<<"Welcome to the database for The School Chemistry"<<endl;

				cout<<"\n\nStudent"		
					<<"\nDetails"<<"\t\tPCHEM 30231 "<<" CHEM 30432 "<<" CHEM30522 "<<" CHEM30532 "<<" CHEM30600 "
					<<endl;
			
				for(int Student=0;Student<nStudents;Student++){	

					if(Data[Student].getSchool()=="School of Chemistry"){
						
						Data[Student].printChemistryBook();	
					
					}	
				}																			break;


			}

	}//Exception try

	catch(char x){
		
		cout<<"\nHow have you managed to mess that up??"
			<<"\nThere is no such option as "<< x

			<<"\n\nDatabase X forgives your error."
			<<"\nPlease pay attention and select an appropriate database to acess."
			<<endl;

 SchoolBook;
	
					}

This post has been edited by dhillon5573: 16 May 2010 - 07:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Repeating a switch in case of a wrong input

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 08:17 PM

there isnt really a need for a try-catch, because you can just catch the exception inside of the switch control structure, but take a look
	char SchoolBook;
	while(true)
	{
		try
		{
                        cout<<"Enter School Book"<<endl;
			cin>>SchoolBook;
			if(SchoolBook < 1 && SchoolBook>3)
				throw SchoolBook;
		}
		catch(char x)
		{

			cout<<"\nHow have you managed to mess that up??"
				<<"\nThere is no such option as "<< x

				<<"\n\nDatabase X forgives your error."
				<<"\nPlease pay attention and select an appropriate database to acess."
				<<endl;
			continue;
		}
		break;
	}
	cout<<"\nYou have selected:\n"<<SchoolBook<<endl;
	switch(SchoolBook)
	{

	case'1':
		cout<<"Welcome to the database for the School of Physics and Astronomy"<<endl;
		cout<<"\n\nStudent"             
			<<"\nDetails"<<"\t\tPHYS 30101 "<<" PHYS 30151 "<<" PHYS 30222 "<<" PHYS 30342 "<<" PHYS 30471 "
			<<endl;
		for(int Student=0;Student<nStudents;Student++){

			if(Data[Student].getSchool()=="School of Physics and Astronomy")
			{
				Data[Student].printPhysicsBook();
			}
		}
		break;
	case '2':
		cout<<"Welcome to the database for The School Chemistry"<<endl;
		cout<<"\n\nStudent"             
			<<"\nDetails"<<"\t\tPCHEM 30231 "<<" CHEM 30432 "<<" CHEM30522 "<<" CHEM30532 "<<" CHEM30600 "
			<<endl;
		for(int Student=0;Student<nStudents;Student++)
		{ 
			if(Data[Student].getSchool()=="School of Chemistry")
			{
				Data[Student].printChemistryBook();     
			}       
		}       
		break;
	}




This post has been edited by ImaSexy: 16 May 2010 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 08:32 PM

Plus using try and catch in this manner in C++ is disingenuous at best. It would be better to rework the loop(s) to repeat on the chance of bad input.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 08:50 PM

In other words, forget about try and catch and instead keep prompting for input until a valid value is supplied. In pseudocode:

while SchoolBook doesn't hold a valid value
    prompt user for input
    read user's input into SchoolBook



See if you can translate that into actual code.

This post has been edited by r.stiltskin: 16 May 2010 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 09:09 PM

View PostImaSexy, on 16 May 2010 - 09:17 PM, said:

there isnt really a need for a try-catch, because you can just catch the exception inside of the switch control structure, but take a look
	char SchoolBook;
	while(true)
	{
		try
		{
                        cout<<"Enter School Book"<<endl;
			cin>>SchoolBook;
			if(SchoolBook < 1 && SchoolBook>3)
				throw SchoolBook;
		}
		catch(char x)
		{

			cout<<"\nHow have you managed to mess that up??"
				<<"\nThere is no such option as "<< x

				<<"\n\nDatabase X forgives your error."
				<<"\nPlease pay attention and select an appropriate database to acess."
				<<endl;
			continue;
		}
		break;
	}
	cout<<"\nYou have selected:\n"<<SchoolBook<<endl;
	switch(SchoolBook)
	{

	case'1':
		cout<<"Welcome to the database for the School of Physics and Astronomy"<<endl;
		cout<<"\n\nStudent"             
			<<"\nDetails"<<"\t\tPHYS 30101 "<<" PHYS 30151 "<<" PHYS 30222 "<<" PHYS 30342 "<<" PHYS 30471 "
			<<endl;
		for(int Student=0;Student<nStudents;Student++){

			if(Data[Student].getSchool()=="School of Physics and Astronomy")
			{
				Data[Student].printPhysicsBook();
			}
		}
		break;
	case '2':
		cout<<"Welcome to the database for The School Chemistry"<<endl;
		cout<<"\n\nStudent"             
			<<"\nDetails"<<"\t\tPCHEM 30231 "<<" CHEM 30432 "<<" CHEM30522 "<<" CHEM30532 "<<" CHEM30600 "
			<<endl;
		for(int Student=0;Student<nStudents;Student++)
		{ 
			if(Data[Student].getSchool()=="School of Chemistry")
			{
				Data[Student].printChemistryBook();     
			}       
		}       
		break;
	}





Is it ok to jump out of a 'try ... catch' structure via 'continue' ?
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 09:12 PM

yupp
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 09:56 PM

View PostImaSexy, on 16 May 2010 - 10:12 PM, said:

yupp


Ok ... but in some languages, like HLA, it isn't, without some special handling.

The OP has already been well advised to simply just test for valid char's ... and that this simple approach *is* the way to code in his program.

This post has been edited by David W: 17 May 2010 - 02:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 10:03 PM

No it's not, a try catch statment is not needed , the exception handling could be handled with the switch
Was This Post Helpful? 0
  • +
  • -

#9 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Repeating a switch in case of a wrong input

Posted 16 May 2010 - 11:36 PM

I would do a
while(bool condition)
{



// all your codes



reverse condition when something is met to leave
}


This post has been edited by IngeniousHax: 16 May 2010 - 11:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 03:07 AM

View PostIngeniousHax, on 17 May 2010 - 12:36 AM, said:

I would do a
while(bool condition)
{

  // all your codes

  // reverse condition when something is met to leave
}




Yes ... that seems to be what we all here at DIC are expressing to the OP.
(Perhaps I was not clear before ... see edit.)

char SchoolBook;
for( ; ; )
{
  cout << "Your input data prompt goes here: " << flush;
  SchoolBook = cin.get();
  cin.sync(); // flush cin stream ...

  if(SchoolBook >= '1' && SchoolBook <= '3')
  {
    // do stuff ... or 
    break; // good data in SchoolBook
  }
  // else ...
  cout << "Some error message ... try again ...\n";
}


Was This Post Helpful? 0
  • +
  • -

#11 dhillon5573  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-April 10

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 06:41 PM

Thank you so much for your replies. The reason i started with the try catch statement was because i thought it would be good programming practice to have seperate code for exceptions. I like how simple the solutions seems to everyone who posted but me. I do not understand how this while(true) condition works. Firstly, while what is true? i tried including the char schoolbook into the parameters to point it out to the compiler and also tried it with just the word true. What results is an infinite loop of the program running over and over. And the funniest bit is my input is recognized as a smiley face that pops up rapidly over n over on the screen. haha.. Can somebody please make it a little clearer for me as i have made quite a lengthy code using polymorphism which took me a month understand and need to get the exceptions input right.


char SchoolBook;
	cin>>SchoolBook;

	while(SchoolBook=true)

	cout<<"\nYou have selected:\n"<<SchoolBook<<endl;

	//if(SchoolBook != '1' && SchoolBook != '2' &&
		//   SchoolBook != '3')
		//	throw SchoolBook;
		
	

	switch(SchoolBook){

		case'1':cout<<"Welcome to the database for the School of Physics and Astronomy"<<endl;

				cout<<"\n\nStudent"		
					<<"\nDetails"<<"\t\tPHYS 30101 "<<" PHYS 30151 "<<" PHYS 30222 "<<" PHYS 30342 "<<" PHYS 30471 "
					<<endl;
			
				for(int Student=0;Student<nStudents;Student++){

					if(Data[Student].getSchool()=="School of Physics and Astronomy"){

							Data[Student].printPhysicsBook();

					}
				}
																							break;

		case '2':cout<<"Welcome to the database for The School Chemistry"<<endl;

				cout<<"\n\nStudent"		
					<<"\nDetails"<<"\t\tPCHEM 30231 "<<" CHEM 30432 "<<" CHEM30522 "<<" CHEM30532 "<<" CHEM30600 "
					<<endl;
			
				for(int Student=0;Student<nStudents;Student++){	

					if(Data[Student].getSchool()=="School of Chemistry"){
						
						Data[Student].printChemistryBook();	
					
					}	
		}																			break;


	}


Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 06:46 PM

while(true)


is an infinite loop. The condition is "true", no evaluation, just the boolean value/constant (for lack of a better word). As opposed to this:

bool notDone = true;

while(notDone){
     //stuff
}



Same deal, but we're using a bool variable that holds true/false



As for your loop:

'=' assignment operator
'==' equality operator
Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 06:46 PM

while(SchoolBook=true)


thats an infinite loop, why are you comparing a char to a boolean?

there is no break within your loop so how do you expect to get out?
Was This Post Helpful? 0
  • +
  • -

#14 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 06:49 PM

I would do something like this or using stringstreams

while(true)
{
  if(cin >> SchoolBook)
    break; // input is good
}

Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Repeating a switch in case of a wrong input

Posted 17 May 2010 - 07:06 PM

Problem is, if the input is bad, then the stream's badbits will be set to true. Then all further tests for good will fail. If I/O fails, you must clear the bits.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2