Missing first letter in getline();

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.

If any one can help me I would appreciate it.

Thanks,
R.

Here is my code:

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
#include<iostream>
#include<string>
#include <iomanip>


using namespace std;


struct PlayerInfo
{
	string	plname;
	int	plnumber;
	int	points_scored;

};

int main()
{
	const int 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;
} 
Never mind I figured it out. The cin.ignore() was in the wrong place.

Thanks!
R.
Topic archived. No new replies allowed.