problem in terminating a for loop containing if else statement

Jan 5, 2015 at 1:25pm
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 Jan 5, 2015 at 1:26pm
Jan 5, 2015 at 4:17pm
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";
    }
}
Jan 5, 2015 at 6:16pm
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 Jan 5, 2015 at 6:21pm
Topic archived. No new replies allowed.