cin doesn't work - skip to next cin

Hello,

After one cycle of while-loop of the 2nd function (addItems) below is finished and I press 'y' to add one more person, it does not ask me to input name, but instead it jumps to Enter ID: .

Here is output screen.
------------------------------------
Enter name: kevin anderson
Enter ID: 1
Enter Salary: 1234
Enter another Employee (y/n)? y
Enter name: Enter ID: //Does not allow user to put name of the second employee
------------------------------------

Can someone help me on this issue?
Thank you.


1st function setEmployee() called by 2nd function...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Employee::setEmployee(){
    string name;
    int idnum;
    double salary;
   
    cout << "Enter name: ";
    std::getline(std::cin, name);
    Name = name;
    
    cout << "Enter ID: ";
    cin >> idnum;
    ID = idnum;

    cout << "Enter Salary: ";
    cin >> salary;
    Salary = salary;
   
}



2nd function addItems...
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
void addItems(List <Employee> &L1)
{
    cout << "Press \"n\" when you are done:\n";
    string answer;
    Employee objInput;
    while (true) // while valid double is entered
    {
        objInput.setEmployee();
        
        if (cin.fail() == false)
        {L1.push_back(objInput);}
        else
        {
            cin.clear(); // clear and ignore bad input
            cin.ignore(80, '\n');
            break; // invalid input
        }
       
        cout << "Enter another Employee (y/n)? ";
        cin >> answer;
        if (answer == "n"){
            cin.clear();
            break;
        }
        else if(answer !="n" && answer != "y"){
            cout << "Invalid input!" << endl;
            cin.clear();
            break;
        }
        
    }
    
}
rationale http://www.cplusplus.com/forum/beginner/157989/#msg808866
solution http://www.cplusplus.com/forum/beginner/24470/#msg129226

I suppose that you may use std::ws if the names cannot start with a space.std::getline( std::cin>>std::ws, name );
Thank you so much for your help.

Topic archived. No new replies allowed.