Saturday, May 22, 2010

C++ booleon problem?

I'm trying to write a program where it asks the user for an input. What I want to do is if the user inputs one of the two specific strings then the program continues on with the rest of the code, but if its anything else, it writes out and error message. Whenever I try and run the program, it displays the error message, even when I enter one of the two acceptable string values. Here is the code:





cin %26gt;%26gt;metric;


if (metric != "ml" || metric != "l")


{


cout %26lt;%26lt;"Incorrect unit entered.\n";


return 0;


}





Also, Im not sure if I have to, but is there some sort of command that I could add under an "else", that just tells the compiler to continue with the rest of the code.

C++ booleon problem?
Assuming metric is a zero-terminated character array, you can't test it using boolean operators in C++. You have to use either the generic C strcmp() function or a more generalized string class.





Regarding your second question, no else is needed. The computer (not the compiler!) will automatically continue executing with the statement after the if.





Hope that helps.

chelsea flower show

No comments:

Post a Comment