2

OK, I have this assignment and I have to prompt the user for data about 5 separate basketball players. My question prompts are in a for loop, the loop executes the first time for the first player fine, but when the second players info needs to be entered the first two question prompts are together on the same line, I have fiddled with this and just cannot figure it out, I am sure it is something small that I am apparently missing, thanks for any suggestions on how to fix this.

Here is the output:

Enter the name, number, and points scored for each of the 5 players.
Enter the name of player # 1: Michael Jordan
Enter the number of player # 1: 23
Enter points scored for player # 1: 64
Enter the name of player # 2: Enter the number of player # 2: <------- * questions 1 and 2 *

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


//struct of Basketball Player info
struct BasketballPlayerInfo
{
    string name; //player name

    int playerNum, //player number
        pointsScored; //points scored

};

int main()
{
    int index; //loop count
    const int numPlayers = 5; //nuymber of players
    BasketballPlayerInfo players[numPlayers]; //Array of players

    //ask user for Basketball Player Info
    cout << "Enter the name, number, and points scored for each of the 5 players.\n";

    for (index = 0; index < numPlayers; index++)
    {
        //collect player name
        cout << "Enter the name of player # " << (index + 1);
        cout << ": ";
        getline(cin, players[index].name);

        //collect players number
        cout << "Enter the number of player # " << (index + 1);
        cout << ": ";
        cin >> players[index].playerNum;

        //collect points scored
        cout << "Enter points scored for player # " << (index + 1);
        cout << ": ";
        cin >> players[index].pointsScored;
    }

 system("pause");
return 0;

}

1 Answer 1

5

After you read a number (e.g., int), there's still a new-line left in the input buffer that you haven't read. When you read another number, that skips across any white-space (including new-lines to find a number. When you read a string, however, the new-line in the input buffer is read as an empty string.

To make it work, you need to get the new-line out of the input buffer before you try to read the string.

Sign up to request clarification or add additional context in comments.

6 Comments

Alternatively, you could simply use cin all three times and be done with it. getline always was a little buggy in my opinion.
@Neil: Presumably you mean using operator >> with cin. In that case, it won't work -- it will only read one word, so (using his example) when the user enters "Michael Jordan", it would read "Michael", but leave "Jordan" in the input buffer, which the program would then try to read as the player number (and obviously fail).
You're obviously right, it has been a while since I programmed basic input in C++.
I am not sure if adding a cin.ignore() at the end of the for loop and now the loop executes properly, not sure if thats the best method but it works. I am a bit confused on the input buffer etc., I need to do some more reading of the documentation.
Yes, that's what Jerry meant. ignore() when called without arguments ignores one character, namely the \n. (And only if it exists).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.