2013-02-03 40 views
0

無論輸入是否正確,下面的代碼都不起作用。如果輸入正確,則if語句仍會因某種原因而執行。任何快速建議都會有所幫助。C++簡單字符檢查

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'; 
} 

回答

3

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

即使這樣的邏輯是有點關閉。你不能鏈邏輯或類似的(或任何邏輯運算符),你應該用別的東西像if(status != 'P' && status != 'R')

2
if ('R') 

結果始終爲true,那麼if(status != 'P' || 'R')結果始終爲true。

變化

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

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

OR

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

的最後一個版本可以給你想要的你更清晰的視野?