logical error in my code

In the following code, I get the logical error when the loop executes for the second time.

During the first time, I'm able to enter the data as required. But during second time, when asking to enter the name, it automatically takes the enter key as input and goes to the next line.

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
#include <iostream>
using namespace std;
struct employee
{
	string name;
	int code;
	unsigned int join_date;
	
};
int main(int argc, char *argv[])
{
	int year,older,num;
	older=2020;
	struct employee a[3];
	cout<<"Enter the employee details. \nFirst enter the first name, then his code, and then the date of joining \n";
	cout<<endl;
	for (int i=0;i<3;i++)
	{
		cout<<"Enter the details of "<<i+1<<" employee\n";
		cout<<"Enter name\n";
		
	//	cin>>a[i].name;
	
		getline(cin,a[i].name);/*it automatically takes enter key as input during i=1*/
	
		cout<<"Enter code\n";
		cin>>a[i].code;
		cout<<"Enter date of joining\n";
		cin>>a[i].join_date;
		if(older>a[i].join_date)
		{
			older=a[i].join_date;
			num=i;
		}
		
	}
	cout<<"Enter the current year :";
	cin>>year;
	cout<<"The oldest person in the company is \n";
	cout<<a[num].name<<endl;
	cout<<a[num].code<<endl;
	cout<<a[num].join_date<<endl;
	
	return 0;
}
Anybody? I don't think it's a difficult problem.
So this is compiling for you without a
#include<string>
?


regarding input, see this:
http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input

Last edited on
Yes, it's compiling without string header file.
Interesting... It does not for me.
Did you read that link? Does it help with the getline/cin stuff?
Topic archived. No new replies allowed.