从控制台读取输入
if(std::cin){
decision = std::cin.get();
if(std::cin.eof())
throw CustomException("Error occurred while reading input\n");
}else{
throw CustomException("Error occurred while reading input\n");
}
清除流中剩余的输入
std::cin.ignore(COL_WIDTH, '\n'); // skip bad input
COL_WIDTH
是通过 Why is the default terminal width 80 characters? 确定的 80 .我应该假设 80 个字符的控制台宽度还是任意更大的数字更适合 cin.ignore
?
最佳答案
你可以输入超过一行的字符,即使是80个。
使用 std::numeric_limits<std::streamsize>::max()
相反。
关于c++ - 控制台宽度;承担违约或成为……?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879879/