0

I am trying to use if else statements to make sure a string has one of two sets of characters, "Yes" or "No". When I run the rest of the code I get to the integer input for the theme, but immediately after inputting the integer the code jumps to the invalid value if statement.

cout << "On a scale of 1-10, how much do you like movies with a similar theme? ";
cin >> theme;
cout << endl;

if (theme < 1 || theme > 10)
{
cout << "Invalid theme value!";
return (-1);
}

cout << "Do you like movies starring the actor or actress that stars in this movie? ";
getline(cin, actor);
cout << endl;

if ((actor != "Yes" && actor != "No"))
{
cout << "Invalid actor value!";
return(-1);
}
else if (actor == "Yes")
{
b = 2.0;
}
else
{
b = 0.0;
}

I've tried going back to just using cin >> but if you input a string such as Yes and no, it recognizes only the Yes and tries to ouput a valid statement.

  • *"when I use getline() and then have an if statement after"* -- did you try the case where you do not have an `if` statement after the `getline()`? (You could use ``cout << "`actor` is \"" << actor << "\"\n";`` as a replacement for the `if`.) When you think you have a complicated situation, it often pays to check if the complications are needed. – JaMiT Jan 31 '23 at 06:36

0 Answers0