c++ - My else if statement using a string is not working -


this question has answer here:

after amount of time trying else if statement work, doesn't. program keeps returning first one, no matter input. please help.

#include <iostream> #include <string> using namespace std;  string arehap;  int main() {     cout << "are happy?" << endl;     cin >> arehap;     if (arehap == "yes" || "y")     {         cout << "good." << endl;     }     else if (arehap == "no" || "n")     {         cout << "bad." << endl;     }     return 0; } 

you should use this:

if (arehap == "yes" || arehap == "y") {     cout << "good." << endl; } else if (arehap == "no" || arehap == "n") {     cout << "bad." << endl; } 

when you're using || operator, have compare 2 boolean values. if arehap equal "y", following statement true: arehap == "y". in case computer "understand" if (true || false) { /* smth */} , evaluate true , code want execute run.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -