problem in terminating a for loop containing if else statement

in the below program the for loop does not get terminated when I enter I,E,i or e.

cin>>b;
for(i=0;i<10;i++)
{if(toupper(b)!='I'||toupper(b)!='E')
{cout<<"enter the correct choice";
cin>>b;}
else if(toupper(b)=='I'||toupper(b)=='E') break;}
Last edited on
if(toupper(b)!='I'||toupper(b)!='E')
This is true for all values of b, including I and E. When b=='I' toupper(b) != 'E' is true, and when b == 'E', toupper(b) != 'I' is true.

You only need to us the second condition:
1
2
3
4
5
6
7
8
for (i = 0; i < 10; i++) {
    cin >> b;
    if (toupper(b) == 'I' || toupper(b) == 'E') {
	break;
    } else {
	cout << "enter the correct choice";
    }
}
thanks :) got the logic. it worked after making minor changes in the program i had to remove the brackets from the break and else statement.after that your solution worked fine
Last edited on
Topic archived. No new replies allowed.