我仍然是C ++的新手(目前正在上一门课程)而且我已经接近完成我的最终项目了。但是这些代码行和我的程序中的其他类似代码导致我的程序在输入两次之后才接受输入。我花了2个小时重新输入它仍然无济于事。
cout <<endl<< "Enter balance:"<<endl;
cin >> balance;
while(!(cin>>balance))
{
cin.clear();
cin.ignore(999,'\n');
cout<<endl<<"Invalid data type! Number expected. Please enter balance again:"<<endl;
}
答案 0 :(得分:0)
摆脱从stdin
读取的双重调用。
cout << endl << "Enter balance:" << endl;
//cin >> balance;
while(!(cin >> balance)) { //This reads in, and continues while read was NOT successful
cin.clear();
cin.ignore(999,'\n');
cout << endl << "Invalid data type! Number expected. Please enter balance again:" << endl;
}