这个问题源自于之前调试AVL树时,要输入两次值,我在第一次cin时,用CTRL+Z结束输入,之后的cin 程序执行直接就跳过了,不能输入了;;;
解决方法: 在第一次输入后的程序加上以下两句时就可以了;
cin.clear(); //cin.sync();
分析:CTRL + Z后;eof、fail 位都会true;clear()之后good位位true;
io标准库条件状态成员;
代码测试:
#include <bits/stdc++.h> using namespace std; int main(){ int num; cout << "first num :" << endl; while(cin >> num) { } cout << "first num is:" << num << endl; cout << "goodbit:" << cin.good() << endl; cout << "eofbit :" << cin.eof() << endl; cout << "failbit:" << cin.fail() << endl; cout << "badbit :" << cin.bad() << endl; cin.clear(); cout << "after clear()" << endl; cout << "goodbit:" << cin.good() << endl; cout << "eofbit :" << cin.eof() << endl; cout << "failbit:" << cin.fail() << endl; cout << "badbit :" << cin.bad() << endl; int tmp; cin >> tmp; cout << "second num: " << tmp << endl; system("pause"); return 0; } /* 没有cin.clear()的输出: first num : 10 ^Z first num is:10 goodbit:0 eofbit :1 failbit:1 badbit :0 second num: 6422400 请按任意键继续. . . */ /* first num : 10 ^Z first num is:10 goodbit:0 eofbit :1 failbit:1 badbit :0 after clear() goodbit:1 eofbit :0 failbit:0 badbit :0 11 second num: 11 请按任意键继续. . . */
标签:cout,CTRL,int,cin,问题,num,输入 From: https://www.cnblogs.com/xuan01/p/17321634.html