cout << "请输入x的值" << endl;
while (cin >> t1) {
x.push_back(t1);
}
cout << "请输入y的值" << endl;
while (cin >> t2){
y.push_back(t2);
}
cout << "请输入r的值" << endl;
cin >> r;
`
逻辑上感觉没错呀,但就是会跳过t2和r的输入。。。。
why?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int t1, t2, r;
vector<int> x, y;
cout << "请输入x的值" << endl;
while (cin >> t1, t1) {
x.push_back(t1);
}
cout << "请输入y的值" << endl;
while (cin >> t2, t2){
y.push_back(t2);
}
cout << "请输入r的值" << endl;
cin >> r;
for (int i = 0; i < y.size(); i++)
cout << y[i] << " ";
cout << "\nr=" << r << endl;
return 0;
}
如果问题得到解决,麻烦点下我回答右边的采纳,谢谢
你输入EOL结束第一个while循环后 cin对象会进入错误状态所以后续的输入会无效,解决方法 在两个while循环后都加上cin.clear()
需要清除当前流的错误状态~