C++ 捕获异常
我想请求用户输入,我通过cin得到的结果如下C++ 捕获异常,c++,exception,iostream,C++,Exception,Iostream,我想请求用户输入,我通过cin得到的结果如下 void AskForGroundstate() { cout << "Please enter an groundstate potential value in Volt:" << endl; if (!(cin >> _VGroundstate)) { cin.clear(); cin.ignore(); cout << "Groun
void AskForGroundstate() {
cout << "Please enter an groundstate potential value in Volt:" << endl;
if (!(cin >> _VGroundstate)) {
cin.clear();
cin.ignore();
cout << "Groundstate potential not valid." << endl;
AskForGroundstate();
}
}
void AskForGroundstate(){
库特(基态){
cin.clear();
cin.ignore();
cout问题在于cin.ignore()
会删除一个字符;您希望将所有字符删除到行尾:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.ignore(std::numeric_limits::max(),'\n');
这可以确保在提示最终用户再次输入之前删除所有无效输入。Clion说,不能解析容器数值限制?@Lucashiede,与标准库中的其他内容一样,您必须包含其标题,
@Lucashiede您需要包含标题。这不是问题,而是以w开头的名称带有下划线,后跟大写字母(\u Vgroundstate
),包含两个下划线的名称保留供实现使用。不要使用它们。