if in a while loop

Well just like the title sames I am having problems with a if statement in a while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;

string operation;
int check=0;

int main()
{
cout<<"Please choose your operation";
		cin>>operation;

		while ( check<1 )
		{
			if(operation=="+")
			{
				check=1;
			}

			else 
			{ 
				cout<<" Sorry you have not choosen the right   operation. Please choose either; +, -, *, and /.";
			}

			cout<<check;	
		}
			
		
		cin.ignore();
		cin.get();
}


the problem is I want the while loop to loop the whole block not just repeat the else when there is a wrong operation. If someone where to click something wrong the while loops just keeps loop else infinitely instead of looping the whole thing all over again. I would like to know why it doesn't loop the whole thing.

thanks in advance

Last edited on
look at your code:
You enter, for example, ! when asced about operation. now your operation variable contains !.

Now you checking if check<1. It does, so you going to if statement. It is false because ! does not equals + so else statement is executed. It will prit long line and thend last statement in while loop. It prints 0, and loop begins anew. Checking if check<1... passs, checking if operation equals +... Wait! operation still contains ! because we didn't bother to change it inside the loop! so it will eternally check if ! equals +, fail and try again on next iteration...
Well you say it like that it really makes me feel silly for missing something like that xD. Thank you for talking the time to explain it so well like that I really appreciate it :).
Topic archived. No new replies allowed.