c++ - My else if statement using a string is not working -
this question has answer here:
- if statement not working right? 5 answers
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
Post a Comment