退出后,C ++程序即使发出哔哔声也会崩溃(0)

时间:2015-03-24 19:37:41

标签: c++ exit

我正在尝试制作一个试图匹配"密码的程序"用户通过一次更改一个字符来引入,从1个字符长的字开始直到20个字符长的字。 这个程序只是作为循环等的练习。 我完全清楚我可以简单地写出" cout<<密码;"但这并不好玩;) 该程序在使用" exit(0);""找到密码后停止。

所以这个奇怪的事情发生在我身上,如果密码超过一个字符长(通常我输入两个字符,因为如果密码很长,那么事情需要花费很多时间)程序保持不变即使在匹配密码并最终崩溃后也会发出哔哔声。 有趣的是,如果这个单词只有一个字符长,它会在匹配密码后停止发出哔哔声并且不会崩溃。

我已经读过,哔哔声可能是打印ASCII字符或类似的东西。我得到了它,但为什么它在程序找到密码后停止发出哔哔声?它仍在后台处理ASCII字符吗?

这里是代码(我很抱歉,如果它有点乱,但你可以从我的解释中了解它的作用,我希望):

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

string get_password()
{
    //Asks for a password between 1 and 20 characters, both included.
    string password;
    do
    {
        cout << "Type a password: ";
        getline( cin, password );
        if ( password.length() > 20 )
        {
            cout << "The password is too long. The maximum number of characters allowed is 20." << endl;
        }
        else if ( password.length() < 1 )
        {
            cout << "The password is too short. The minimum number of characters allowed is 1." << endl;
        }
    } while ( password.length() < 1 || password.length() > 20 );
    return password;
}

void guess(string password)
{
    //Password 'cracking' happens here.
    int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16, i17, i18, i19, i20;
    char c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15, c16, c17, c18, c19, c20;
    string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, ss;
    for ( i1 = 1; i1 < 126; i1++ )
    {
        if ( i1 > 2 )
        {
            c1 = i1;
            s1 = c1;
        }
        for ( i2 = 1; i2 < 126; i2++ )
        {
            if ( i2 > 2 )
            {
            c2 = i2;
            s2 = c2;
            }
            for ( i3 = 1; i3 < 126; i3++ )
            {
                if ( i3 > 2 )
                {
                    c3 = i3;
                    s3 = c3;
                }
                for ( i4 = 1; i4 < 126; i4++ )
                {
                    if ( i4 > 2 )
                    {
                        c4 = i4;
                        s4 = c4;
                    }
                    for ( i5 = 1; i5 < 126; i5++ )
                    {
                        if ( i5 > 2 )
                        {
                            c5 = i5;
                            s5 = c5;
                        }
                        for ( i6 = 1; i6 < 126; i6++ )
                        {
                            if ( i6 > 2 )
                            {
                                c6 = i6;
                                s6 = c6;
                            }
                            for ( i7 = 1; i7 < 126; i7++ )
                            {
                                if ( i7 > 2 )
                                {
                                    c7 = i7;
                                    s7 = c7;
                                }
                                for ( i8 = 1; i8 < 126; i8++ )
                                {
                                    if ( i8 > 2 )
                                    {
                                        c8 = i8;
                                        s8 = c8;
                                    }
                                    for ( i9 = 1; i9 < 126; i9++ )
                                    {
                                        if ( i9 > 2 )
                                        {
                                            c9 = i9;
                                            s9 = c9;
                                        }
                                        for ( i10 = 1; i10 < 126; i10++ )
                                        {
                                            if ( i10 > 2 )
                                            {
                                                c10 = i10;
                                                s10 = c10;
                                            }
                                            for ( i11 = 1; i11 < 126; i11++ )
                                            {
                                                if ( i11 > 2 )
                                                {
                                                    c11 = i11;
                                                    s11 = c11;
                                                }
                                                for ( i12 = 1; i12 < 126; i12++ )
                                                {
                                                    if ( i12 > 2 )
                                                    {
                                                        c12 = i12;
                                                        s12 = c12;
                                                    }
                                                    for ( i13 = 1; i13 < 126; i13++ )
                                                    {
                                                        if ( i13 > 2 )
                                                        {
                                                            c13 = i13;
                                                            s13 = c13;
                                                        }
                                                        for ( i14 = 1; i14 < 126; i14++ )
                                                        {
                                                            if ( i14 > 2 )
                                                            {
                                                                c14 = i14;
                                                                s14 = c14;
                                                            }
                                                            for ( i15 = 1; i15 < 126; i15++ )
                                                            {
                                                                if ( i15 > 2 )
                                                                {
                                                                    c15 = i15;
                                                                    s15 = c15;
                                                                }
                                                                for ( i16 = 1; i16 < 126; i16++ )
                                                                {
                                                                    if ( i16 > 2 )
                                                                    {
                                                                        c16 = i16;
                                                                        s16 = c16;
                                                                    }
                                                                    for ( i17 = 1; i17 < 126; i17++ )
                                                                    {
                                                                        if ( i17 > 2 )
                                                                        {
                                                                            c17 = i17;
                                                                            s17 = c17;
                                                                        }
                                                                        for ( i18 = 1; i18 < 126; i18++ )
                                                                        {
                                                                            if ( i18 > 2 )
                                                                            {
                                                                                c18 = i18;
                                                                                s18 = c18;
                                                                            }
                                                                            for ( i19 = 1; i19 < 126; i19++ )
                                                                            {
                                                                                if ( i19 > 2 )
                                                                                {
                                                                                    c19 = i19;
                                                                                    s19 = c19;
                                                                                }
                                                                                for ( i20 = 1; i20 < 126; i20++ )
                                                                                {
                                                                                    c20 = i20;
                                                                                    s20 = c20;
                                                                                    ss = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 + s14 + s15 + s16 + s17 + s18 + s19 + s20;
                                                                                    if ( ss != password )
                                                                                    {
                                                                                        cout << ss << " : Access denied." << endl;
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        cout << ss << " : Access granted." << endl;
                                                                                        exit(0);
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

int main()
{
    //Type in a password.
    string password = get_password();
    //Trying to get your password.
    guess(password);
}

1 个答案:

答案 0 :(得分:0)

您正在打印不可打印的字符。具体来说,你的循环生成ASCII值低于32的字符,其中包括BEL字符,这些字符在“打印”时会发出蜂鸣声。

在程序终止后它一直发出哔哔声的原因很可能是缓冲了你的控制台:你的程序生成的输出比控制台可以显示的速度快,所以输出被写入缓冲区并从那里打印出来;一旦你的程序完成,缓冲区仍然需要打印出来。