> In using C++, how do I use a while statement for invalid answers then switch to in if statement once a valid input has b

In using C++, how do I use a while statement for invalid answers then switch to in if statement once a valid input has b

Posted at: 2014-12-18 
I gather you are talking about something like this:

? ? here: ? ? ? // get user 'input'

? ? ? ? ? ? ? ? ? ? if ( input == optionA ) {

? ? ? ? ? ? ? ? ? ? ? ? // do optionA stuff;

? ? ? ? ? ? ? ? ? ? } else if ( input == optionB ) {

? ? ? ? ? ? ? ? ? ? ? ? // do optionB stuff;

? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? // output error message

? ? ? ? ? ? ? ? ? ? ? ? goto here;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? // fall through here if optionA or optionB was selected and performed

? ? ? ? ? ? ? ? ? ? // so that you can do something else.

Another approach is like this:

? ? here: ? ? ? // get user 'input'

? ? ? ? ? ? ? ? ? ? if ( input != optionA && input != optionB ) {

? ? ? ? ? ? ? ? ? ? ? ? // output error message

? ? ? ? ? ? ? ? ? ? ? ? goto here;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? if ( input == optionA ) {

? ? ? ? ? ? ? ? ? ? ? ? // do optionA stuff;

? ? ? ? ? ? ? ? ? ? } else // must be optionB then

? ? ? ? ? ? ? ? ? ? ? ? // do optionB stuff;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? // fall through here.

This second approach moves the code that performs the options outside the loop structure itself. Not materially different from the first option, except that you have to test twice -- once to find out if there is an error message, which means testing ALL valid options at once and again to find out which bit of code to actually execute, later. It's cleaner to use the first method, because adding new options means only adding that option, modifying the code just once, instead of modifying the code in two places which you have to do in the 2nd case. Otherwise, the two illustrations above are essentially identical.

Typically, though, the process repeats (you want to allow the user to do repeat the options. The above examples don't illustrate that.

In C++, you can do something like:

? ? ? ? for ( ; ; ) {

? ? ? ? ? ? // get user 'input'

? ? ? ? ? ? if ( input == optionA ) {

? ? ? ? ? ? ? ? // do optionA stuff;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? } else if ( input == optionB ) {

? ? ? ? ? ? ? ? // do optionB stuff;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? ? ? // output error message

? ? ? ? }

? ? ? ? // fall through here if options were valid and performed.

Note that the break statements will exit the loop. There is no need for a final 'else' clause, since ALL of the valid options break the loop.

However, all this is getting into an area of "style" now. You can just as well use do {} while ( true ), or, while ( true ) {}, as well as the above mentioned for (;;) {} approach. All of these avoid the use of goto and a label and all work well. The main thing now is that once you decide what you "prefer" here, that you STICK WITH IT when writing your code. Don't flip/flop around, using one today and a different one tomorrow. You will really screw up your readers that way. Just pick a method and stick with it. Then readers will figure you out and follow along okay.

if you're in a loop in C derived languages the "break;" command will imediately exit just outside the loop end, while the "continue;" command will start with the next loop iteration.

So in your case you'll have:

while(true) {

... get user input here ...

if (option == 1) {

... do your stuff for option 1 here ...

break;

}

if (option == 2) {

... do your stuff for option 2 here ...

break;

}

... handle "Invalid command" here ...

}

bool rightAnswer = false;

while(!rightAnswer){

// get input

//change rightAnswer

}

try this code.

#include

using namespace std;

int main() {

int ch;

while(1) {

cout<
cin>>ch;

if(ch==1) {

cout<
}

else if( ch==2) {

cout<
}

else if( ch==3)

break;

else

cout<
}

return 0;

}

NO TAN!!!!!!!!!!!

I'm writing a little program and want the user to choose between two different options. If they type in one statement, it goes to the first part of an if statement, while if they type in the other, it goes to an if else statement. Which I can do. But what I want to happen, is if they input an invalid command to one of the if statements, the program responds saying Invalid Command. And repeats the process until the user enters a valid command where it then goes to the if statements. This is what I cannot figure out how to do.