So I'm having a slight problem at line 128. I call a set function to try
and set the name of the first player but for some reason the function
"SetPlayerName" on line 32 skips player 1's call to the function...BUT
the function call works for all the other players. Can someone give me a
little insight to what is going on. Thanks!
Oh! And the function call does work....only halfway. e.g. When player1 [p1]
calls the function SetPlayer name, it only outputs half of the function
which is "what is your name: "
/*___________________________________"WHO GUESSED THE BEST???"___________________________________
- There will be five rounds
- The player who guesses closest to the hidden number wins
- If no one has guessed the correct answer within 5 rounds, the player with the highest score wins
- To score high points, a player has to consistently guess very close to the hidden number!
- If the player misses the hidden numnber by 1, then that player will get a hint the next time around as to what the hidden number is
void SetPoints(int& x)///set points for having a good guess
m_points += x;
int GetPoints()///returns a players points
void SetPlayerName()///sets the players name
cout<<"what is your name: ";
int EvaluatePlayersGuess(int& x)
if(x == m_hiddenNumber)///if the player guesses the number
//playerName has guessed the hidden number....write code for this
elseif((x == m_hiddenNumber -1) || (x == m_hiddenNumber +1))///if players's guess is just shy of the hidden number by 1
//playerName gets 25 points added to their score
elseif((x >= m_hiddenNumber - 10) && (x <= m_hiddenNumber + 10) && (x != m_hiddenNumber))///if player's guess is shy by ten
//playerName gets 20 points added to their score
m_hiddenNumber = rand() % 50 + 1;
cout<<"(1.) There will be five rounds\n"
<<"(2.) The player who guesses the hidden number wins\n"
<<"(3.) If game ends w/out number being guessed, player with highest score wins\n"
<<"(4.) To score high points, consistently guess very close to the hidden number\n"
<<"(5.) *** Players receives hints when they guess really close to the number ***\n";
string m_name;///specifies the name of the character
int m_hiddenNumber;///the random number
int m_points;///the points of each player
int m_numOfPlayersToPlay;///specifies how many players the user specifies will play
int main()/**________________________________________THE MAIN FUNCTION________________________________________**/
GET_SET_CLASS universal;///object to call everything that does not have to do with the players
cout<<"________________Welcome to * Who Guessed The Best *________________"<<endl<<endl;
cout<<"Who Guessed The Best is a 4 player game\n\n"
<<"How many players will be playing? ";
while(numOfPlayersPlayingTheGame > 4 || numOfPlayersPlayingTheGame < 1)///fail check for entering number of players
cout<<"Sorry...Only 1 through 4 players can play\n"
<<"Enter again: ";
///The next four if/else's get the players names
if(numOfPlayersPlayingTheGame > 0)
cout<<"Player 1 ";
p1.SetPlayerName();//come back to this because for some reason I cannot enter a name for player 1
if(numOfPlayersPlayingTheGame > 1)///if number of players is 2, set player 2's name
cout<<"Player 2 ";
if(numOfPlayersPlayingTheGame > 2)///if number of players is 3, set player 3's name
cout<<"Player 3 ";
if(numOfPlayersPlayingTheGame > 3)///if number of players is 4, set player 4's name
cout<<"Player 4 ";
///Everybody's name is set after this
cout<<"Press enter when you are ready "<<p1.GetPlayerName()<<endl<<endl;
cout<<"-- Press Enter to Continue --";
cout<<"-- When everyone is ready press enter to continue --";
Your issue is all of the cin >> ... statements. It leaves "junk" in the input buffer and it forces the program to act like it's ignoring player one. The is a few alternatives, but try using cin.ignore(80, '\n'); after each of these lines, or switching them from cin >> ... to cin.get(...); or cin.getline(...) instead. This is a known issue for beginners.
cin.ignore(80,'\n') clears the input buffer either until it has gone through 80 characters or it runs into a new line character. (Which is defined as '\n'). This is to make sure nothing is left in the buffer after cin >> for getline to read.
The common way to remove everything from the input buffer is cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Just to make sure that it will clear the buffer no matter the size of it, but it shouldn't make much difference for your code. You may need to #include <limits> for the above example.
cin.clear() clears the error flags, not the input buffer.
I believe there are times when cin.clear() doesn't work, but since I don't use it, I don't know what the issues with it are. The cin.ignore(80,'\n') ignores up to the next 80 characters in the cin buffer or the firs new line character, which ever comes first. There is a long explanation to why this is needed, but just remember that the new line character is displayed differently on each operating system. Sometimes junk gets stored in the buffer that was supposed to be cleared by cin, but sometimes the operating systems send two characters, thus messing up cin and sitting in the buffer to wreak havoc in your program later on, as in your case.