Reputation: 1944
while (1)
{
cout << "Enter the Citizen ID number of the worker or Enter 0 to exit:" << endl;
getline (cin, j);
for (i=0; i<5; i++)
{
if (workers[i]->IDno == j)
{
wFind = 1;
cout << "Choose your option:" << endl;
cout << "1- Display all details of the worker" << endl;
cout << "2- Display number of the days worker delayed" << endl;
cout << "3- Display number of the days worker missed" << endl;
cin >> k;
switch (k)
{
case 1:
workers[i]->AWorker();
break;
case 2:
cout << workers[i]->TotalDaysDelayed() << endl;
break;
case 3:
cout << workers[i]->TotalDaysMissed() << endl;
break;
default:
break;
}
}
else
wFind = 0;
}
if (wFind == 0)
cout << "ERROR: No worker has the ID number that you typed!" << endl;
}
Note: wFind
is initialized as 2
.
When I execute this code, I always get this output:
Enter the Citizen ID number of the worker or Enter 0 to exit:
ERROR: No worker has the ID number that you typed!
Enter the Citizen ID number of the worker or Enter 0 to exit:
Interestingly enough, my code can complete a cycle in the loop before getting my input for the string j
. How can this be possible and what can I do to fix it?
Upvotes: 0
Views: 119
Reputation: 44191
cin >> k;
will not eat the newline. You need to call std::getline
after it to consume the newline.
If you're having the problem on the first iteration of the loop, I would presume you have code above this which does not eat the newline as well.
Upvotes: 1