d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Question > Allowing For Reentry Following Invalid 1
Add Reply New Topic New Poll
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Sep 25 2013 05:47pm
I have to make a menu and you choose which one you want by pressing 1 2 or 3, however, this is required:

"Check that the user has selected one of the available menu choices. If they have not, allow re-entry before the switch is executed"

my guess is to do:

if(medium!=1)
if(medium!=2)
if(medium!=3){

cout<< "Invalid entry, please reenter" << endl;

}

Unsure how to enable the user to reenter following an invalid entry.

While loop perhaps? I tried, but was unsuccessful because I was still not able to reenter the selection.

Here is my code:

float distance;
float air = 1100;
float water = 4900;
float steel = 16400;


cout << " 1 - Air 2 - Water 3 - Steel" << endl;
cin >> medium;

switch(medium){
case 1:
cout << " You have selected air \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/air << endl;
break;

case 2:
cout << " You have selected water \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/water << endl;
break;

case 3:
cout << " You have selected steel \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/steel << endl;
break;

}

return 0;
}


Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 25 2013 06:02pm
Code
while (1)
{
if (cin >> n)
{
if ((n>=2) && (n<=9))
{
cout << "There will be " << n << " players playing.\n";
break;
}
else
{
cout << "Please insert a number between 2-9.\n";
}
}
else
{
cout << "Not a valid input, try again..." << endl;
cin.clear();
while (cin.get() != '\n'); //keeps the program from going on forever, empty loop
}
}


this is an example of how i got mine to work.. this problem was to insert a # between 2-9. my code above works for both inserting characters instead of numbers and any number outside of the 2-9 range.

hope that helps
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 26 2013 07:13pm
Your code is fine, just add the condition to go back to the start of the switch in case no valid case is found, like this:

Quote

start:
cout << " 1 - Air 2 - Water 3 - Steel" << endl;
cin >> medium;

switch(medium){
case 1:
cout << " You have selected air \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/air << endl;
break;

case 2:
cout << " You have selected water \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/water << endl;
break;

case 3:
cout << " You have selected steel \n" << endl;
cout << " How many feet will the sound wave travel in this medium? \n" << endl;
cin >> distance;

cout << " The total time it will take, in seconds, is " << distance/steel << endl;
break;


default:
cout << "wrong selection, try again";
goto start;


}

return 0;
}
Member
Posts: 17,856
Joined: Apr 4 2012
Gold: 59.31
Sep 29 2013 07:44pm
very quick question!
Member
Posts: 1,087
Joined: May 2 2005
Gold: 75.00
Oct 2 2013 09:53pm
Goto is almost entirely outlawed in most school courses and is poor programming. You can wrap the menu in a loop that can then act as a counter controlled loop. I would strongly recommend avoiding the use of goto unless your intention is spaghetti code.

Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Oct 2 2013 10:35pm
Quote (ligs @ Oct 3 2013 05:53am)
Goto is almost entirely outlawed in most school courses and is poor programming. You can wrap the menu in a loop that can then act as a counter controlled loop. I would strongly recommend avoiding the use of goto unless your intention is spaghetti code.


It's the most explicit representation of a jump operation in asm. This is C, not C++ or some managed fancy new language with internal spaghetti you can't even fathom.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Oct 3 2013 07:03am
i'd do it like this

Code
while(true){
cout << " 1 - Air 2 - Water 3 - Steel" << endl;
cin >> medium;
if(medium >= 1 && medium <= 3 ){
break;
}
cout<< "Invalid entry, please reenter" << endl;
}


Member
Posts: 7,969
Joined: Jul 7 2008
Gold: 286.54
Oct 3 2013 11:01pm
Quote (labatymo @ Oct 3 2013 05:03am)
i'd do it like this

Code
while(true){
cout << " 1 - Air 2 - Water 3 - Steel" << endl;
cin >> medium;
if(medium >= 1 && medium <= 3 ){
break;
}
cout<< "Invalid entry, please reenter" << endl;
}


Yeah, this is how I'd do it.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll