C++简单的字符检查

C++ Simple Char Check

本文关键字:检查 字符 简单 C++      更新时间:2023-10-16
无论

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

char status;
cout<<"Please enter the customer's status: ";
cin>>status;
if(status != 'P' || 'R')
{
    cout<<"nnThe 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';
}
这是

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

即便如此,逻辑还是有点不对劲。你不能像这样链接逻辑或(或任何逻辑运算符),你可能应该使用其他类似if(status != 'P' && status != 'R')

if ('R')

计算结果始终为 true,因此if(status != 'P' || 'R')的计算结果始终为 true。

改变

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

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

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

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