```Hello everyone. 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: "```

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171`` ``````/*___________________________________"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 */ #include #include #include #include #include #include using namespace std; class GET_SET_CLASS { public: void SetPoints(int& x)///set points for having a good guess { m_points += x; } int GetPoints()///returns a players points { return m_points; } void SetPlayerName()///sets the players name { cout<<"what is your name: "; getline(cin, m_name); } string GetPlayerName() { return m_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 void StatScreen(); return 0; } else if((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 } else if((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 } } void SetRandomNumber() { m_hiddenNumber = rand() % 50 + 1; } int GetRandomNumber() { return m_hiddenNumber; } void Instructions() { 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"; } private: 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 GET_SET_CLASS p1; GET_SET_CLASS p2; GET_SET_CLASS p3; GET_SET_CLASS p4; int numOfPlayersPlayingTheGame; cout<<"________________Welcome to * Who Guessed The Best *________________"<>numOfPlayersPlayingTheGame; cout< 4 || numOfPlayersPlayingTheGame < 1)///fail check for entering number of players { cout<>numOfPlayersPlayingTheGame; } cout< 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 "; p2.SetPlayerName(); } if(numOfPlayersPlayingTheGame > 2)///if number of players is 3, set player 3's name { cout<<"Player 3 "; p3.SetPlayerName(); } if(numOfPlayersPlayingTheGame > 3)///if number of players is 4, set player 4's name { cout<<"Player 4 "; p4.SetPlayerName(); } ///Everybody's name is set after this cout<
Last edited on
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.
Alright thanks. Will try asap.
It worked well! What exactly did using "cin.ignore(80,'\n')" do?
Also I think I am confused about cin.clear() because I thought that would clear the buffer also...
Last edited on
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.

http://www.cplusplus.com/reference/iostream/ios/clear/
http://www.cplusplus.com/reference/iostream/istream/ignore/
Last edited on
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.
Alright will do. Thanks that really helped.
cin.clear() never clears the buffer. All it does it clear any errors that are set.
Topic archived. No new replies allowed.