[英]if condition on turbo C++
I'm having a problem regarding with if statement in C++ this statement is in do-while loop. 我在使用C ++中的if语句时遇到问题这个语句是在do-while循环中。
gotoxy(27,22);cout<<"Do you want to continue [Y]?";
sub=getche();
if(sub!='y' || sub!='Y')
{
gotoxy(27,24);cout<<"INVALID ANSWER!!";
gotoxy(27,26);cout<<"Closing Program....";
delay(3000);
exit(1);
}else
{
sub=ans;
}
}while(tolower(ans)=='y');
whenever I input y on the variable sub the code on if statement is still executing.. please someone tells me where is the error.. Thanks! 每当我在变量sub上输入y时,if语句上的代码仍在执行..请有人告诉我哪里出错...谢谢!
The boolean expression of (sub!='y' || sub!='Y')
will always evaluate to true (sub!='y' || sub!='Y')
的布尔表达式将始终求值为true
This line: 这一行:
if(sub!='y' || sub!='Y')
Needs to be this: 需要这样:
if ( (sub != 'y') && (sub != 'Y') )
Your code if(sub!='y' || sub!='Y')
will be true ,no matter what you enter because either sub!='y'
or sub!='Y'
will evaluate to true. 你的代码if(sub!='y' || sub!='Y')
将是真的,无论你输入什么,因为sub!='y'
或sub!='Y'
将评估为true。 Hence Use &&
instead of ||
因此使用&&
而不是||
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.