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;
}
注:wFind
は、2
として初期化されます。私はこのコードを実行するとループは入力を取得する前にサイクルを完了し、修正する方法は?
、私はいつもこの出力を得る:興味深いことに
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:
、私のコードは、文字列j
のための私の入力を取得する前にループでサイクルを完了することができます。どのようにこれを可能にすることができますし、私はそれを修正するために何ができますか?
getlineとcinを使用して入力バッファをクリアしたいとします。ときどきそこに少し空白があり、入力をさらに、特に整数に分割します。それはかなりではありませんが、私のために次のことができました: 'while(cin.peek()<''){cin.ignore(1); } ' –
トークンの抽出(' '' ')と行の読み込み(' 'getline')を混在させないでください。 –