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.
#include <iostream>
usingnamespace std;
struct employee
{
string name;
int code;
unsignedint 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;
}