简体   繁体   English

结束字符替换的 while 循环

[英]End while loop for character substitution

This is taken out from Keyshanc Encryption Algorithm.这是从 Keyshanc 加密算法中取出的。 https://github.com/Networc/keyshanc https://github.com/Networkorc/keyshanc

My question is: How can I possibly manipulate this main method for having multiple encryption outputs with the keys chosen from the password array?我的问题是:如何使用从密码数组中选择的密钥来操作这个主要方法来获得多个加密输出?

I am not able to break out of the encoding loop right at the end.我无法在最后跳出编码循环。

int main()
{
    string password[] = {"JaneAusten", "MarkTwain", "CharlesDickens", "ArthurConanDoyle"};

    for(int i=0;i<4;++i)
    {
        char keys[95];

        keyshanc(keys, password[i]); 

        char inputChar, trueChar=NULL;
        cout << "Enter characters to test the encoding; enter # to quit:\n";
        cin>>inputChar;

        for (int x=0; x < 95; ++x)
        {
            if (keys[x] == inputChar)
            {
                trueChar = char(x+32);
                break;
            }
        }

        while (inputChar != '#')
        {
            cout<<trueChar;
            cin>>inputChar;
            for (int x=0; x < 95; ++x)
            {
                if (keys[x] == inputChar)
                {
                    trueChar = char(x+32);
                    break;
                }
            }

        }
     }
    return 0;
}

You are taking input in two places, so you have to put two tests.您在两个地方接受输入,因此您必须进行两个测试。

When you are in the while loop, you have to break out of the while loop, and also break out of the for(;;) loop.当你在while循环中时,你必须跳出while循环,也跳出for(;;)循环。 You can set i=5;你可以设置i=5; to force the for(;;) loop to stop.强制for(;;)循环停止。

for (int i = 0; i<4; ++i)
{
    char inputChar, trueChar = 0;
    cout << "Enter characters to test the encoding; enter # to quit:\n";

    cin >> inputChar;

    if (inputChar == '#') //<== ADD THIS
        break;

    while (inputChar != '#')
    {
        cout << trueChar;
        cin >> inputChar;

        if (inputChar == '#') //<== ADD THIS to break out of for(;;) loop
        {
            i = 5;
        }

    }
}

In addition trueChar and inputChar should be initialized to 0 not NULL (although it's the same thing in this).此外, trueCharinputChar应该初始化为0而不是NULL (尽管在此是相同的)。 Don't print trueChar if it's not initialized.如果未初始化,则不要打印trueChar

if (trueChar)
    cout << trueChar;

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

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