I have this working like it should (I think) except that it is cutting off the first letter of the first display name unless I add a space before the name. I have tried using just using

cin >> soccer[i].name

but it doesn't work, it skips over the name entering the second time through the loop. Here is my code, any help would be awesome!

#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct SoccerPlayer
    {
           string name;
           int num;
           int pts;
    };

int main(int argc, char *argv[])
{
    const int team = 2;
    SoccerPlayer soccer[team];
    int i;
    int total = 0;
    int highest = 0;
    int pNum;
    string pName;

    for (i = 0; i < team; i++)                      
    {
        cout << "Enter player # " << (i + 1) <<  "'s full name:  ";
        cout << endl;
        cin.ignore();
        cout << endl;
        getline(cin, soccer[i].name);
        cout << endl;

        cout << "Enter player # " << (i + 1) <<  "'s jersey number: ";
        cout << endl;
        cin >> soccer[i].num;
            if (soccer[i].num < 0)
            {
               cout << "Invalid entry:  Enter a positive number.\n";
               cin >> soccer[i].num;
            }
        cout << endl;

        cout << "How many points did player # " << (i + 1) <<  " score?: ";
        cout << endl;
        cin >> soccer[i].pts;
            if (soccer[i].pts < 0)
            {
               cout << "Invalid entry:  Enter a positive number.\n";
               cin >> soccer[i].pts;
            }

        total += soccer[i].pts;
        cout << endl;
    }

    cout << "Player Name           Player Number           Points Scored";
    cout << endl;
    cout << "___________________________________________________________";
    cout << endl;

    for (i = 0; i < team; i++)
    {
        cout << left << setw(25) << soccer[i].name;
        cout << setw(25) << soccer[i].num;
        cout << setw(25) << soccer[i].pts;
        cout << endl;
    }

    cout << endl;
    cout << "The total points scored by the team was " << total;
    cout << endl;


    for (i = 0; i < team; i++)
    {
      if (soccer[i].pts > highest)
         {
             highest =  soccer[i].pts;
             pNum = soccer[i].num;
             pName = soccer[i].name;
         }
    }

    cout << endl;
    cout << "The player who scored the most points is: " << pName;
    cout << endl;
    cout << "That player's jersey number is: " << pNum;
    cout << endl;
    cout << "That player scored " << highest << " points.";
    cout << endl;


    system("PAUSE");
    return EXIT_SUCCESS;
}

Your problem is here:

cout << "Enter player # " << (i + 1) << "'s full name: ";
cout << endl;
cin.ignore();
cout << endl;
getline(cin, soccer[i].name);
cout << endl;

The cin.ignore(); is ignoring the first character typed. Also all those endl's aren't necessary. Try this instead:

cout << "Enter player # " << (i + 1) << "'s full name: ";
getline(cin, soccer[i].name);

This way the typed name will display on the same line right after the colon and the enter key will echo a new line when it's typed.

I would suggest going little further and have a struct/class representing the team. Now each team, which contains a collection of players, can have it's own stats. A league class, with a collection of teams, would wrap the whole thing up. If you create your classes to represent the real world you will find it more intuitive, when you use the classes and input the data.

Edited 2 Years Ago by tinstaafl

If I leave out the cin.ignore(); I can only enter the name the first time through the loop it skips over it every other time through the loop.

Tinstaafl is correct. You need to remove the cin.ignore() at line 29. That does not need to be there.

But wherever you use cin to get input from the user (i.e. using cin >>), you need to use a call to cin.ignore() immediately afterwards. But it's not necessary to use ignore after using std::getline.

So put a call to cin.ignore() between lines 36 and 37, 40 and 41, 46 and 47 etc...

Edited 2 Years Ago by JasonHippy

I don't think the .ignore( ) is needed after 36 or 40, only after the last extraction operation before the loop goes back to getline. So only at line 52.

Thank you all who responded. Vmanes I added it at line 52 and it works great now. Thanks

This question has already been answered. Start a new discussion instead.