Program ignoring Getline

I was working on a problem given in my course book. The target was to create a struct to hold the serial number, the name and the phone number of 10 Members, all the data to be given by the user, and then present the data on the screen.

I made the program do everything asked in the question, but there is a problem.
The problematic part is this code segment:
1
2
3
4
5
6
7
8
for (int i = 0;i<10;i++)
{
	Memdat[i].SerialNumber = i+1;
	cout << endl << "Serial Number: " << Memdat[i].SerialNumber << endl; 
	cout << "Please enter the name of the Member:  ";
	getline (cin,Memdat[i].Name);
	//More code...
}

As you can see, I am using getline to get the user's name into a string in an object Memdat[10], of type Member. Name is of type string.

When I run the program, it asks for the name the first time, but then ignores it.
That is:

Serial Number: 
1
Please enter the name of the Member:  
Nisheeth
Please enter the number of the Member:
............

Serial Number: 
2
Please enter the name of the Member:  
Please enter the number of the Member:
............


and so on.
Can someone tell me what am I doing wrong, and how can I correct it?
{I am using MS VC++ Express 2010 in Windows 7 64-Bit}
When you do cin >> something, only the relevant characters are extracted. Since the input stream contained 1 and an newline (because you pressed enter), after >> extracts the 1, newline remains. Then this newline if found by getline and thus the first getline seems to be skipped. To avoid this, add cin.ignore() after cin >>.
Thanks a lot for explaining the problem and how to solve it!
Topic archived. No new replies allowed.