putback()函数请教
程序代码:
#include<iostream> using namespace std; char getchar(istream &in=cin ) { char ch; while (in.peek() != EOF && ((ch = in.get() == ' ') || ch == '\t')); return ch; } int main(void) { char ch; double x; ch = getchar(cin); while (ch!='\n') { if ((ch >= '0')&&(ch <= '9')) { cin.putback(ch); cin >> x; cout << x << " "; } else { cout << "出现非法字符!" << endl; exit(1); } ch = getchar(cin); } cout << endl; system("pause"); return 0; }
为什么输入什么都是出现非法字符?