All answers are counted as correct on my trivia game

I'm currently working on my first game, a text-based trivia game about myself. However, no matter what the player enters, the game counts it as correct. I am using Dev-C++. Here is the code:
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
//text based game
#include <iostream>
#include <string>
#include <sstream>
#include <new>
using namespace std;
int main ()
{ string player;
char answer1 []="Y";
char answer2 []="Bill";
char answer3 []="America";
char answer4 []="Pablo";
cout << "What do you want to be called? ";
getline (cin,player);
cout << endl;
cout << "Hello " << player << ". We're going to play a three question trivia game about me. Do you accept? (Y/N) ";
if (cin >> answer1)
cout << endl << "What is my first name? ";
else
{ cout << player << "is a loser! " << endl;
}
if (cin >> answer2)
cout << endl << "What country was I born in? ";
else
{ cout << player << "is a loser! " << endl;
}
if (cin >> answer3)
cout << endl << "What's my Spanish name? ";
else
{ cout << player << "is a loser! " << endl;
}
if (cin >> answer4)
cout << endl << "You win! " << endl;
else
{ cout << player << "is a loser! " << endl;
}
system("pause"); return 0; }


I believe that the problem is specifically in the following line, which is used four different times:
cout << player << "is a loser! " << endl;

Again, the game won't stop no matter what you put, and it will always say "You win!" after you answer all three questions. Any help would be appreciated.

Also, I can't figure out how to use the Debug function on my Dev-C++ compiler. I raised both the game and the Debug question to my father, an experienced programmer, but he has not used C++ for several years and the Dev-C++ Debugger is different than anything he ever used (or he forgot).
The problem is when you do cin>>answer1, etc. you are reading into those arrays, not reading data then comparing it to answer1. You need to do something like this:
1
2
3
4
5
6
7
8
const std::string answer1("Y");
std::string input;
std::getline(std::cin, input);
if(input == answer1) {
    // stuff
} else {
    // other stuff
}
closed account (3pj6b7Xj)
I love it when folks take the C++ syntax and turn it upside down yet still managed to get the program running. That creates bugs that are are near impossible to find in a massive project.

cin >> answer1 gets player input when you state if (cin >> answer1) you are evaluating the condition of cin >> answer1 which in this case will always be true when something is typed, you aren't comparing your answers at all...you starting walking but you left your legs behind you, lol!

Like Zhuge said, compare your input to your answer, don't compare the input to itself.
Thanks for the help Zhuge! This piece of code here works like a charm:
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
//text based game
#include <iostream>
#include <string>
#include <sstream>
#include <new>
using namespace std;
int main ()
{ string player;
const std::string answer1("Y");
const std::string answer2("Bill");
const std::string answer3("America");
const std::string answer4("Pablo");
std::string input1;
std::string input2;
std::string input3;
std::string input4;
cout << "What do you want to be called? ";
getline (cin,player);
cout << endl;
cout << "Hello " << player << ". We're going to play a three question trivia game about me. Do you accept? (Y/N) ";
std::getline(std::cin, input1);
if(input1 == answer1)
cout << "Great! " << endl;
else
cout << player << " is a loser! " << endl;
cout << endl << "What is my first name? ";
std::getline(std::cin, input2);
if(input2 == answer2)
cout << "Creeper! " << endl;
else
cout << player << " is a loser! " << endl;
cout << endl << "What country was I born in? ";
std::getline(std::cin, input3);
if(input3 == answer3)
cout << "Have you seen my papers? " << endl;
else
cout << player << " is a loser! " << endl;
cout << endl << "What's my Spanish name? ";
std::getline(std::cin, input4);
if(input4 == answer4)
cout << endl << "Are you in my class? " << endl;
else
cout << player << " is a loser! " << endl;
if (input1 == answer1, input2 == answer2, input3 == answer3, input4 == answer4)
cout << "Congrats " << player << ", you won!" << endl;
else
cout << "Try better next time " << player << ". " << endl;
system("pause"); return 0; }


Now that the question is solved, I have another. I understand the code you gave me except for the std:: parts. Could you please explain it to me?

And to mrfaosfx, thanks for clearing that up.

EDIT: Changed up the code a bit for a better little game.
Last edited on
Topic archived. No new replies allowed.