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:
//text based game
#include <iostream>
#include <string>
#include <sstream>
#include <new>
usingnamespace 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:
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.