Too Many cin Inputs Required

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

int main()
{
string a, b, 
answery="Too bad, no cookie!", 
answerdefault="I don't understand.";

	cout << "Would you like a cookie?";
	cin >> a;
	if (a=="y"||"yes")
		cout << answery;
	else if (a=="n"||"no"){
		cout << "Are you sure?";
		cin >> b;
if (b=="y"||"yes")
cout << "Fine, be that way.";
else if (b=="n"||"no")
cout << answery;
else
cout << answerdefault;}
else
cout << answerdefault;
	return 0;
}
Hey. This is how I would have done the program -


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

char a, b;
	string answery = "Too bad, no cookie!",
	answerdefault = "I don't understand.";

	cout << "Would you like a cookie? (Y/N) ";
	cin >> a;

	if (a == 'Y' || a == 'y')
	{
		cout << answery << endl;
	}

	else if (a == 'N' || a == 'n')
	{
		cout << "Are you sure?";
		cin >> b;

		if (b == 'Y' || b == 'y')
		{
			cout << endl << "Fine, be that way." << endl;
		}
		else if (b == 'N' || b == 'n')
		{
			cout << answery << endl;
		}

		else
		{
			cout << answerdefault;
		}
	}


If there is anything you dont understand. Feel free to ask.
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.
Last edited on
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.
Last edited on
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.
Last edited on
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?
Topic archived. No new replies allowed.