Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

c++ - if statement not working right?

I've looked and looked with the debugger and cannot seem to figure out why the IF statement always prints the message.

The IF statement checks to see if yesno != 'Y' ||(or) 'N' but regardless if i type y or Y or n N or H B it will show... . Im not sure what move to make anymore! I cant seem to find where its going wrong?

if(yesno != 'Y' || 'N') { ...

Thanks guys.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The || doesn't quite mean what you think it means. The correct approach is:

if (yesno != 'Y' && yesno != 'N') { ...

This evaluates each side of the && independently, and the result is true if both sides are true.

Note that

if (yesno != 'Y' || yesno != 'N') { ...

will always be true because any given character is either not Y or it is not N. This is probably not what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...