【发布时间】:2011-10-15 05:16:54
【问题描述】:
只是尝试比较两个用户定义的向量,看看它们是否相等,当前代码:
vector<int> ivec1, ivec2; //vectors, uninitialized
int temp1;
cout << "Enter integers to be stored in ivec1." << endl;
while(cin >> temp1) //takes input from user and creates new element in the vector to store it
{
ivec1.push_back(temp1);
}
int temp2;
cout << "Enter integers to be stored in ivec2." << endl;
while(cin >> temp2) //same as above with different vector
{
ivec2.push_back(temp2);
}
if(ivec1 == ivec2)
cout << "ivec1 and ivec2 are equal!" << endl;
else
cout << "ivec1 and ivec2 are NOT equal!" << endl;
到目前为止,它让我可以为 ivec1 赋值,但是当我通过输入一个字母使 cin 失败退出 while 循环时,它会跳过第二个 while 块。出于好奇,我尝试在第一个 while 循环之后放入其他 cin 语句,它也将它们全部忽略。
强制 cin 失败会导致程序忽略所有其他对它的调用或其他什么,还是有其他问题?如果是这样,我怎样才能让这个程序做我想做的事?
截图供您观赏: http://img695.imageshack.us/img695/2677/cinfailure.png
*PS。拥有 temp1 和 temp2 只是我想弄清楚是否对两个分配循环使用相同的 int 是否会导致问题,无论如何我只是想我会把它留在那里
【问题讨论】:
标签: c++ loops while-loop cin