Question skipping right to answer?

Having an issue with the last question in my code. itll skip right to the answer and say "who is getting into college? its me!" instead of letting me try answering (dont make fun of me ok i need this for a college portfolio thought itd b cute)

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 #include <iostream>

using namespace std;

int main()
{
	string str;
	cout << "Enter your name: ";
	cin >> str;
	cout << "Would you like to solve a riddle," << str << "? Y/N: ";
	char answer;
	cin >> answer;

	if (answer == 'y' || answer == 'Y')
	{
		cout << "Wonderful!";
	}
	else if (answer == 'n' || answer == 'N')
	{
		cout << "Well, thats no fun";
	}
	else
	{
		cout << "sorry, i dont understand! try Y or N!";
	}
	cout << "\n" "what runs around the whole yard without moving?: ";
	cin >> answer;

	if (answer == 'f' || answer == 'F')
	{
		cout << "yes! a fence! very good. here's one more riddle!";
	}
	else
	{
		cout << "no! sorry, the answer was: a fence! lets try a different question";
	}
	cout << "\n" "who is getting accepted into college? : ";
	cin >> answer;

	if (answer=='m' || answer=='M')
	{
		cout << "Thats right! me!";
	}
	else
	{
		cout << "its me!";
	}
}
Your input after entering the name is likely not going to be a single character, "fence" for example. That is going to bork up the input stream, multiple characters left in the input stream when you extract a single character at a time.

There is a way around that, using std::getline for the string. Using getline also allows the user to enter a name that has spaces:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>  // use C++ string, include the header!

int main()
{
	std::string str;

	std::cout << "Enter your name: ";
	std::getline(std::cin, str);
	
	std::cout << "\nWould you like to solve a riddle, " << str << "? Y/N: ";
	std::getline(std::cin, str);

	if (str[0] == 'y' || str[0] == 'Y')
	{
		std::cout << "Wonderful!\n";
	}
	else if (str[0] == 'n' || str[0] == 'N')
	{
		std::cout << "Well, that's no fun\n";
		return 0; // if they don't want to play, bail!
	}
	else
	{
		std::cout << "Sorry, I dont understand! try Y or N!\n";
	}
	
	std::cout << "\nWhat runs around the whole yard without moving ? : ";
	std::getline(std::cin, str);

	if (str[0] == 'f' || str[0] == 'F')
	{
		std::cout << "Yes! A fence! Very good. Here's one more riddle!\n";
	}
	else
	{
		std::cout << "No! Sorry, the answer was: a fence! lets try a different question\n";
	}
	
	std::cout << "\nWho is getting accepted into college ? : ";
	std::getline(std::cin, str);

	if (str[0] == 'm' || str[0] == 'M')
	{
		std::cout << "That's right! Me!\n";
	}
	else
	{
		std::cout << "No, it's me!\n";
	}
}
Enter your name: George P

Would you like to solve a riddle, George P? Y/N: y
Wonderful!

What runs around the whole yard without moving ? : fence
Yes! A fence! Very good. Here's one more riddle!

Who is getting accepted into college ? : you
No, it's me!

FYI, if later you mix using std::cin's operator>> and std::getline there will be similar problems to what you have now, left-over data in the buffer will clobber the extraction.
https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdstring/

Lines 14-25: If you don't recognize the answer, you tell the user to enter Y or N, but then go on to the next question. You need to loop until they enter an answer you recognize.

Lines 29-36: ditto

Topic archived. No new replies allowed.