您需要從清除線CIN,使用cin.ignore,除了清除流狀態(這是什麼cin.clear一樣)。
我有several utility functions,使這個容易(你會感興趣clearline特別,它清除了流狀態和當前行)和what you want幾乎一個確切的例子。
您的代碼,或多或少的,用我的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的是
std::basic_istream<char, std::char_traits<char> >
- 性病的一個typedef :: wistream是的
std::basic_istream<wchar_t, std::char_traits<wchar_t> >
- 一個typedef擴大允許
'\n'
成爲L'\n'
適當
- 該代碼同時適用於的常見焦炭和wchar_t的箱子,也
- 它寫入到被稱爲
clearline(stream)
或stream >> clearline
basic_istream的任何兼容的實例,比較其它操縱像的std :: ENDL,的std :: WS,或的std :: boolalpha
謝謝!這解決了它。還有一點是我必須同時使用cin.clear()和cin.ignore()。 – 2010-01-09 07:45:04
是的,如果你看看我的clearline函數(在clinput.hpp中,並按照代碼路徑),它會調用clear和ignore。 – 2010-01-09 07:45:46
+1由於明顯的原因 – 2010-01-09 08:02:39