I am writing a small program that ask a user to type in a players name, number and points score and display on the screen. The program is using struct with an array. When I run the code the first player name is missing the first letter. I am not sure why this is. I think it has to do with the cin.ignore() but I do not know how to fix it.
#include<iostream>
#include<string>
#include <iomanip>
usingnamespace std;
struct PlayerInfo
{
string plname;
int plnumber;
int points_scored;
};
int main()
{
constint TEAM_PLAYER = 5; //Number of team players
PlayerInfo player [TEAM_PLAYER]; //Array of structures
int index;
int total = 0;
//Enter the player information
cout << "Enter the " << TEAM_PLAYER << " players information" << endl;
for(index = 0; index < TEAM_PLAYER; index++)
{
//Get the player name
cout << "What is number " << (index + 1) << " player name: ";
cin.ignore();
getline(cin, player[index].plname);
//Get the player number
cout << "What is number " << (index + 1) << " player number: ";
cin >> player[index].plnumber;
//Get the player score
cout << "What is number " << (index + 1) << " player score; ";
cin >> player[index].points_scored;
cout << endl;
}
//Display all the players names, number and scores
//Also display the total points scored
cout << "Here are the " << TEAM_PLAYER << " players" << endl;
cout << "======================================================================" << endl;
cout << "#" << setw(20) << "Player Name" << setw(20) << "Player Number" << setw(20) << "Points Scored" << endl;
for(index = 0; index < TEAM_PLAYER; index++)
{
cout << (index + 1) << setw(20) <<player[index].plname << setw(15) << player[index].plnumber << setw(20) << player[index].points_scored << endl;
//Get the total score of all the players
total += player[index].points_scored;
}
cout << "======================================================================" << endl;
cout << endl;
cout << "Total points scored by all " << TEAM_PLAYER << "players is " << total << endl;
system("pause");
return 0;
}