while loop

can someone help me fix the while loop in my program. What i want it to do is to ask the user input the operation until it is either "+","-,"*","/". "operation is a constant char.

1
2
3
4
5
6
7
while (operation=='+','-','*','/')
          {     cout<< "You have chosen" <<operation;
          
                else {
cout<< "Wrong operation please enter again";
                     cin >> operation;
                }
closed account (jwC5fSEw)
You can't check for equality using a list. You need to seperate them with OR:

if ((operation == '+') || (operation == '-') || ...) // and so on
Last edited on
I fixed it but i still need help with the else part.

1
2
3
4
5
6
7
while ((operation == '+') || (operation == '-') || (operation == '*') || (operation == '/'))
          {     cout<< "You have chosen" <<operation;
          
                else
                     cout<< "Wrong operation please enter again";
                     cin >> operation;
                }
Do i open a new curly bracket?
Um, a while isn't a conditional so it doesn't have an else.
closed account (jwC5fSEw)
Oh yeah, you don't want to be using while here, you want to use if. I can't believe I missed that.
Well, I kind of fixed it but it doesn't loop if the condition is true; in this case it is if it is not equal to +-*/.

The revised code:

1
2
3
4
5
while ((!operation == '+') || (!operation == '-') || (!operation == '*') || (!operation == '/'));
          {
                     cout<< "Wrong operation please enter again";
                     cin >> operation;
                     }



The result when compiled:
Enter the expression you wish to calculate

For example: '1*2'
1#3
Wrong operation please enter again# // it is supposed to repeat until operation is either +-*/
The answer is 7.06871e+268
Last edited on
closed account (jwC5fSEw)
Put the ! operator outside of the condition (i.e. !(operation == 'x')). The way it is now, you're not checking if operation is not equal to the character; you're checking if the opposite of operation (which is gonna be false, assuming operation isn't 0) is equal to the character.
Topic archived. No new replies allowed.