0

无论输入是否正确,下面的代码都不起作用。如果输入正确,则 if 语句由于某种原因仍会执行。任何快速的建议都会有所帮助。

char status;

cout<<"Please enter the customer's status: ";
cin>>status;

if(status != 'P' || 'R')
{

    cout<<"\n\nThe customer status code you input does not match one of the choices.\nThe calculations that follow are based on the applicant being a Regular customer."<<endl;

    status='R';
}
4

2 回答 2

3

if(status != 'P' || status != 'R')

即使这样,逻辑也有点不对劲。您不能像那样链接逻辑 OR(或任何逻辑运算符),您可能应该使用其他类似的东西if(status != 'P' && status != 'R')

于 2013-02-03T03:42:42.580 回答
2
if ('R')

总是计算为真,所以if(status != 'P' || 'R')总是计算为真。

改变

if(status != 'P' || 'R')

if(status != 'P' && status != 'R')

或者

if(status == 'P' || status == 'R')

最后一个版本可能会让您更清楚地了解您想要什么?

于 2013-02-03T03:43:06.853 回答