Problem with getline()

I've writen this code. But the first getline() only acts only one time when i=0.
For the second time it doesn't act. Do you know why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

 void main()
{
	string name,surname;
	int salary,i=0;

	for(i=0;i<2;i++)
	{
		cout<<"Enter name: ";
		getline(cin,name);

		cout<<"Enter surname: ";
		getline(cin,surname);

		cout<<"Enter salary per month: ";
		cin>>salary;

	}
}


Output:

Enter name: david
Enter surname: alen
Enter salary per month: 10
Enter name: Enter surname:




Last edited on
Do this
1
2
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

after line 25

This is due to stray/extra characters on the input stream
Thank you codewalker.
It is solved.
Topic archived. No new replies allowed.