-
newbie cin help
Ok im new at this and trying to learn but im having problems trying to write an or function for cin. Heres what im trying to do but the or function doesnt work. It will run but it always comes up false.
Code:
cin.getline ( resp1, 50 );
if ( strcmp ( resp1, "answer1 || answer2") == 0)
cout<<"Outputted text"<<"\n";
else
cout<<"outputted text"<<"\n";
I Hope someone can help me out. if you have any ideas or think you can help let me know.
-
Let me guess: you want to check if resp1 is equal to "answer1" or "answer2". The solution is to write:
Code:
if (strcmp(resp1, "answer1") == 0 || strcmp(resp1, "answer2") == 0)
But really, you should be using std::string, in which case your entire snippet could be simplified to:
Code:
getline(cin, resp1);
if (resp1 == "answer1" || resp1 == "answer2")
cout << "Outputted text\n";
else
cout << "outputted text\n";
-
Laserlight you are amazing. thanks for the fast help. Not only did you help me solve my issue but you also made it much more simplified.