只是试图比较两个用户定义的向量,看看它们是否相等,当前代码:
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
*附注。拥有 temp1 和 temp2 只是我试图弄清楚是否对两个分配循环使用相同的 int 会导致问题,无论如何我只是想我会把它留在那里