I am trying to write a simple program to better acquaint myself with if, else if, and else statements. The program works as expected up until the first response to cin >> a is generated, but the program continues after that, requiring 2 additional cin inputs before it terminates, each one giving the response corresponding to cin >> b. Is there something I am missing regarding how these statements work?
Thank you for your input. Perhaps I should reword my question, however, as that leaves me with the same problem. I am trying to figure out why the program does not terminate after displaying "Too bad, no cookie!" if the first input is "y" or "yes", even though the if condition has been met, and likewise, why it does not terminate after the response to the second input if the first is "n" or "no," and if there is a way to remedy that.
Your disjunctions are not written properly: a == "y" || "yes" evaluates two things: a == "y", which checks if a is the string "y", and "yes", which checks if the string "yes" evaluates to true. It just so happens the "yes" does evaluate to true; always, in fact, so that statement will always been considered true. You could even write: if(true) {
and get the same effect.
However, you obviously meant to check a against both strings; in that case, you have to write each condition individually: if(a == "y" || a == "yes")
Start by fixing this and see if it helps solve your problem.
That is, essentially, the same answer that TarikNeaj gave (only with more explanation), and unfortunately it still leaves me with the same problem. I have tried rewriting it many different ways, even removing later instances of cin, but it still requires 3 inputs before the program terminates.
I only see you asking for input twice in your example. Could you post your current code as well as an example of input/output of the program that demonstrates the problem?