stumped about getline

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: "


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*___________________________________"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 <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
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 *________________"<<endl<<endl;
    universal.Instructions();
    cout<<endl<<endl;

    cout<<"Who Guessed The Best is a 4 player game\n\n"
        <<"How many players will be playing? ";
    cin>>numOfPlayersPlayingTheGame;
    cout<<endl;

    while(numOfPlayersPlayingTheGame > 4 || numOfPlayersPlayingTheGame < 1)///fail check for entering number of players
    {
        cout<<endl;
        cout<<"Sorry...Only 1 through 4 players can play\n"
            <<"Enter again: ";
        cin>>numOfPlayersPlayingTheGame;
    }

    cout<<endl;


    ///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 ";
        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<<endl<<endl;

    if(numOfPlayersPlayingTheGame==1)
    {
        cout<<"Press enter when you are ready "<<p1.GetPlayerName()<<endl<<endl;
        cout<<"-- Press Enter to Continue --";
        cin.ignore();
    }

    else
    {
        cout<<"-- When everyone is ready press enter to continue --";
        cin.ignore();
    }






    return 0;
}
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.