在 while 循环中滥用 cin 进行 int 赋值

发布于 2024-11-26 22:51:45 字数 1097 浏览 1 评论 0原文

只是尝试比较两个用户定义的向量以查看它们是否相等,当前代码:

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 循环之后放入其他 cin 语句,它也忽略它们。

强制 cin 失败是否会导致程序忽略对其的所有其他调用或其他问题,或者是否还有其他问题?如果是这样,我怎样才能让这个程序做我想做的事?

截图供您观赏: http://img695.imageshack.us/img695/2677/cinfailure.png

*附注。拥有 temp1 和 temp2 只是我试图弄清楚两个赋值循环使用相同的 int 是否会导致问题,无论如何我只是想把它留在那里

simply trying to compare two user defined vectors to see if they are equal, current code:

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;

So far it lets me assign values to ivec1 just fine, but as I exit the while loop by entering a letter to make cin fail, it skips the second while block. Out of curiosity I tried putting in other cin statements after the first while loop, and it ignores them all as well.

Does forcing cin to fail cause the program to ignore all other calls for it or something, or is there another problem? If so, how can I get this program to do what I want?

screenshot for your viewing pleasure:
http://img695.imageshack.us/img695/2677/cinfailure.png

*PS. having temp1 and temp2 was just me trying to figure out if using the same int for both assignment loops was causing the problem, anyway I just figured I'd leave it there

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凡尘雨 2024-12-03 22:51:45

您必须执行 cin.clear() 来重置流状态。然后,您必须确保从流中读取有问题的字符(使用描述的技术之一此处),这样下一次输入操作也不会失败。

You would have to do cin.clear() to reset the stream state. Then you will have to make sure that the offending character is read from the stream (using one of the techniques described here), so that the next input operation does not fail as well.

神经大条 2024-12-03 22:51:45

您的意思是您执行 ctrl-D 来为第一个循环提供文件结尾。

问题是,一旦达到 EOF,它将持续存在,第二个循环也将看到 EOF,但不会读取任何内容。

而是使用终止字符,例如空行或“.”并专门在玩具 while 循环中测试它,而不是 while (cin >> tmp1)

You mean that you a doing a ctrl-D to give end-of-file for the first loop.

The problem with that is that once EOF is achived it will persist and the second loop will also see the EOF and never read anything.

Instead use a terminating charater such as a blank line or a '.' and specifically test for that in toy while loop instead of while (cin >> tmp1)

浮云落日 2024-12-03 22:51:45

在循环之间使用 cin.clear() 。此命令将流的状态重置回可用状态。

知道您并不总是需要输入无效字符来退出循环可能会有所帮助,您还可以在控制台上使用(在 Windows 上)ctrl-z (在其他系统上为 ctrl-d),这会刺激 EOF 。您仍然需要 cin.clear() (因为 EOF 仍然会使流无效) - 但它并不那么危险

Use cin.clear() between the loops. This command resets the state of the stream back to a usable one.

Might be helpful to know that you don't always have to enter an invalid character to exit a loop, you can also use (on windows) a ctrl-z (ctrl-d on other systems) on the console, which stimulates an EOF. You'd still have to cin.clear() (because an EOF still invalidates the stream) - but it's not as dangerous

画尸师 2024-12-03 22:51:45

first while 循环由于 std::cin 失败而退出时,它还会在内部设置失败标志。您只需在第一个 while 循环之后写入以下内容即可清除该标志:

scin.clear();

它会清除所有失败标志,以便 cin 可以用于读取进一步的输入。

When the first while loop exits because of failure of std::cin, it also sets the failure flag internally. All you need to clear that flag by writing the following after the first while loop:

scin.clear();

It clears all the failure flag, so that cin can be used to read further inputs.

兲鉂ぱ嘚淚 2024-12-03 22:51:45

当我解决同样的问题时我发现了这一点。我必须添加 cin.clear() 和 cin.ignore() 来重置循环之间的流并让它再次识别“cin”调用。

I found this when I was working through the same problem. I had to add cin.clear() and cin.ignore() to reset the stream between loops and have it recognize the 'cin' calls again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文