除了清除流状态(这是cin.clear所做的)之外,您还需要使用cin.ignore清除cin中的行。
我有几个实用函数可以让这更容易(你会特别对clearline感兴趣,它会清除流状态和当前行),并且几乎是你想要的一个确切的例子。
您的代码或多或少使用我的clearline:
#include "clinput.hpp" // move my file to a location it can be used from
int main() {
using namespace std;
while (true) {
cout << "Enter a number (0 to exit): ";
int number;
if (cin >> number) {
cout << "Read " << number << '\n';
if (number == 0) {
break;
}
}
else {
if (cin.eof()) { // tested only *after* failed state
cerr << "Input failed due to EOF, exiting.\n";
return 1;
}
cerr << "Input failed, try again.\n";
clearline(cin); // "cin >> clearline" is identical
}
}
return 0;
}
这里仍然存在一个潜在问题(在我的clinput_loop.cpp中使用blankline修复),将输入留在缓冲区中,这将搞砸以后的 IO(参见示例会话中的“42 abc”)。将上面的代码提取到一个单独且自包含的函数中作为练习留给读者,但这里有一个框架:
template<class Type, class Ch, class ChTr>
Type read(std::basic_istream<Ch,ChTr>& stream, Ch const* prompt) {
Type value;
// *try input here*
if (could_not_get_input or more_of_line_left) {
throw std::runtime_error("...");
}
return value;
}
template<class Type, class Ch, class ChTr>
void read_into(
Type& value,
std::basic_istream<Ch,ChTr>& stream,
Ch const* prompt
) {
value = read<Type>(stream, prompt);
}
示例使用:
int n;
try {
read_into(n, std::cin, "Enter a number: ");
}
catch (std::runtime_error& e) {
//...
raise;
}
cout << "Read " << n << '\n';
为后代提取的clearline函数,以防上述链接中断(并稍作更改以使其独立):
#include <istream>
#include <limits>
template<class C, class T>
std::basic_istream<C,T>& clearline(std::basic_istream<C,T>& s) {
s.clear();
s.ignore(std::numeric_limits<std::streamsize>::max(), s.widen('\n'))
return s;
}
如果您不习惯,模板的东西会有点混乱,但这并不难:
- std::istream是一个 typedef
std::basic_istream<char, std::char_traits<char> >
- std::wistream是一个 typedef
std::basic_istream<wchar_t, std::char_traits<wchar_t> >
- 扩大允许
'\n'
变得L'\n'
适当
- 此代码适用于常见的char和wchar_t情况,但也适用于basic_istream的任何兼容实例化
- 它被称为
clearline(stream)
or ,与 std::endl、std::ws或std::boolalphastream >> clearline
等其他操纵器相比