so when i have my if statement, if someone goes to put in say: 1 for the answer, and 1 wasnt a choice, when i put it in again it closes the program, heres an example

int action;
cout << "press 2, 3, or 4." << endl;
cin >> action;
if (action == 2)
cout << "you picked 2" << endl;
else if (action == 3)
cout << "you picked 3" << endl;
else if (action == 4)
cout << "you picked 4" << endl;
else cin >> action

how do i make it so if they were to put in 1.
then it would wait for them to put in 2 3 or 4 to output something?

Need a while loop.

One way would be to initialize action to 1, then put a while loop between lines 1 and 2.

while(action == 1)
{
//All your code
}

This way it will keep repeating until they enter 2, 3 or 4.

Play around with it and see what you can come up with.

Edited 6 Years Ago by LevyDee: n/a

or could i make it where:

while(action /= 2| action /= 3  | action  /=4
{
// code here
}

so it says if action does not equal 2 3 or 4

Edited 6 Years Ago by skorm909: n/a

Yes, except your loop parameters are going to be != instead of /=. /= is not a valid operand. And your pipes are going to be || instead of |.

while(action != 2 || action != 3 || action != 4)
{
//code
}

Im sorry, you need to use && instead of ||. Had to double check myself.

Edited 6 Years Ago by LevyDee: n/a

Yes, except your loop parameters are going to be != instead of /=. /= is not a valid operand. And your pipes are going to be || instead of |.

Actually, '/=' is a valid operand. The statement " x /= y; " is shorthand for dividing x by y then saving the result of the operation back to x. It's similar to (but not identical to) x = x / y; . Either way, in this situation, it is not what the OP wants.

@OP:
1. To say "not-equal-to" you need to use '!="
2. You are using bitwise ORs, you need logical ORs. A logical OR is a double pipe '||' not a single.
3. For your situation though, you would be stuck in an infinite loop if you write it with ORs. You actually need to do 1 of 2 things:
a. use logical ANDs instead of ORs (that is '&&')
b. change the condition to (x < 2 || x > 4)

Edited 6 Years Ago by Fbody: n/a

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