简体   繁体   English

为什么这个程序无限循环?

[英]Why does this program go in endless loop?

I have such a program: 我有这样一个程序:

#include <stdlib.h>
#include <iostream>

static int pswd=0;

int main() {

    do {
        std::cout<<"I need your password:"<<std::endl;          
        std::cin>>pswd;
    } while (pswd!=3855);

    std::cout<<"Congratulations! Your password is correct! Your soul is free again!"<<std::endl;
}

And I have, may be, a stupid question. 我可能是一个愚蠢的问题。 When I enter invalid values (with non-numerical symbols or values greater then int) program goes in endless loop without reading of any information from console. 当我输入无效值(非数字符号或大于int的值)时,程序进入无限循环而不从控制台读取任何信息。

I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
I need your password:
        ...

Why does this program go in endless loop? 为什么这个程序无限循环?

Because after invalid input, the stream is in failed state and all further input operations are no-op. 因为在无效输入之后,流处于失败状态,并且所有进一步的输入操作都是无操作。 You always have to check the result of the input operation. 您始终必须检查输入操作的结果。

do {
    std::cout<<"I need your password:"<<std::endl;          
    if (!(std::cin >> pswd)) {
        // clear error flags
        std::cin.clear();
        // discard erroneous input (include <limits>)
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
} while (pswd!=3855);

It is trying to read an int but it can take a peek into the buffer from STDIN. 它试图读取一个int但它可以从STDIN中查看缓冲区。 It notices that you have not got an int so the cin>> fails. 它注意到你没有int所以cin>>失败了。 (See fail bit ). (见失败位 )。

So it just goes around again. 所以它再次出现。 You need to check for failed type conversion. 您需要检查失败的类型转换。

I'm pretty sure you want to be reading in a string here, as there's nothing to control what the user is typing. 我很确定你想在这里阅读一个字符串,因为没有什么可以控制用户输入的内容。

You want to read into a char buffer (the one below supports 256 characters) and then compare it with the password you're looking for using strcmp : 你想读入一个char缓冲区(下面的一个支持256个字符),然后将它与你正在寻找使用strcmp的密码进行比较:

#include <stdlib.h>
#include <iostream.h>

static int pswd=0;
static char buffer[256];

int main()
{
    do
    {
        std::cout<<"I need your password:"<<std::endl;          
        std::cin>>buffer;
    }
    while (strcmp("3855", buffer));
    std::cout<<"Congratulations! Your password is correct! Your soul is free again!"<<std::endl;
}

Note that strcmp returns 0 when two strings match. 请注意,当两个字符串匹配时, strcmp返回0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM