-1

I am relatively new to code and I experimenting with if, else if, and else statements. I wrote the code below but every time I type in one of the possible inputs, the else statement occurs. For example, for the input I wrote how about you? but the output was "Sorry, either you made a typo or I currently don't have a response to that" instead of "i'm doing decent."

string c;
std::cin >> c;
if (c == "how about you?")
{
   cout << "i'm doing decent \nthanks for asking";
}
else if (c == "How about you?")
{
   cout << "i'm doing decent \nthanks for asking";
}
else if (c == "how about you")
{
   cout << "i'm doing decent \nthanks for asking";
}
else if (c == "how bout you")
{
   cout << "i'm doing decent \nthanks for asking";
}
else
{
   cout << "Sorry, either you made a typo or I currently don't have a response to that \nthank you for your time";
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sam Ly
  • 1
  • You may be interested in [How to convert std::string to lower case?](https://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case). – François Andrieux Dec 14 '18 at 00:24
  • With user input like this it is often worth printing the input out as part of the message to see what the computer things is being tested. `cout << "Got >" << c << "<\n";` – Martin York Dec 14 '18 at 00:26
  • “You are in a maze of twisty little passages, all alike.” I like your experiment. – Pete Becker Dec 14 '18 at 01:00

2 Answers2

3

std::cin will stop reading once it encounters a whitespace. So even though you entered "how about you?", only "how" is stored in string c. This is the reason why else part is getting executed.

kadina
  • 5,042
  • 4
  • 42
  • 83
3

Use getline(cin, c) instead reference

donpsabance
  • 358
  • 3
  • 9