Hi all,

Quick question: This is the only thing left I'm trying to work out on my console program. I am trying to return control back to the point where a user enters their selection after giving an invalid entry. I am using switch statements that house the selections, and I'm a little vague on how to allow them to re-enter a correct selection under the 'default' switch.

Here is the piece of code I'm having trouble with:

char Derived::getSelection(char selection)
{
	cout <<"\nPlease enter your selection: ";
	cin >> selection;
	selection = toupper(selection);
	cin.get();

	switch (selection)
	{
	    case 'A':
	         cout <<"\nYou have selected New Game";    //no functionality yet
	         break;
            case 'B':
                 cout <<"\nYou have selected Load Game";   //no functionality yet
                 break;
            case 'C':
                 showHiScore(HiScore);
                 break;
	    default:
		 cout <<"\nInvalid selection. Please re-enter your selection: ";
		 cin >> selection;
	}
	return selection;	
	//getSelection() definition
}

Any advice or direction would be appreciated greatly.

use a arbitrary while loop like this.

int loop=0;
while(loop!=1){
cin>>selection;
switch(selection){
case 'A':
	cout<<"blah blah blah";
	loop=1;
	break;
case 'B':
	cout<<"yada yada yada";
	loop=1;
	break;
default:
	cout<<"bad input try again: ";
	}

no problem need anymore help just let me know unless i cant help you(my knowledge is that of a intermediate programmer - my Nub ness) though i have been programming in C/C++ since i was 15 i got 2 years under my belt lol.
glad to see that it worked for you.

This article has been dead for over six months. Start a new discussion instead.