I've made a program which when the 'if' and 'else' statements are used, here's the code...The output is almost working, when I input 'no' it displays 'bad' as it should, but when I input 'yes' it displays both Good and Bad? does anyone have any ideas? Which is a rhetorical question cause you guys are all probably aware of what the problem is unlike me;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//random9
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string response;
cout << "Do you like me?\n";
cin >> response;
if (response == "yes") {
cout << "Good.\n";
}
else (response == "no");{
cout << "Bad.\n";
}
}
try using elseif (response == "no") //and no semicolon on line 15. and also, you're leaving a hole in your program where if the user types something other than the expected text in, nothing happens. if you are going to check for multiple scenarios, it's a good idea to check for all of them.
Quick last question, I added this end to it but now whenever I take the semi colon from line 25, the output also puts both quotes in. When i input the "I don't know" without the semi colon, it does not output the goodbye on it's own, but when I put the semi colon back in, the "goodbye" displays as expected, but then when I input the quote "Carion" with the semi colon it displays both 'cout' quotes?...any ideas?
the reason it's not working is because cin >> response only takes input up until the first space. what you should use is getline(cin, response) which takes in all the characters inputted, no matter how many spaces there are. so, on line 21, replace cin >> with getline(cin, response).