简单密码停止循环工作

Simple cipher stops working in loop

对于第一个 运行,密码将加密字符串。但是如果我想再次循环,我无法得到密码来加密第二个字符串。

有没有办法不用指针就可以做到这一点?

#include <iostream>
#include <string>
using namespace std;

char checkSum[63] = "QAZXSWEDCVFRTGBNHYUJMKIOLPqazxswedcvfrtgbnhyujmkiolp1234567890"; //checksum
string message; //original message
string messageEncrypted; //replaces pointer
char YorN;
//int multiplier; //part of the prototype

int main(int argc, char *argz[])
{


    do
    {
        cout << "enter a message to encrypt: ";
        getline(cin, message);
        messageEncrypted = message; //i could use a pointer for this i think but I keep running into problems

        for (unsigned count = 0; count <= messageEncrypted.length(); count++) //cycles through characters in string
        {

            //multiplier = messageEncrypted.length() + (int)messageEncrypted[count]; //declaring prototype
            //messageEncrypted[count] = (int)messageEncrypted[count] * multiplier; //prototyped idea that doesn't work

            while ((int)messageEncrypted[count] > 62) //checks to make sure it is withen the value range of checkSum
            {
                messageEncrypted[count] -= ('A' - 3); //puts it into that range
            }

            messageEncrypted[count] = checkSum[(int)messageEncrypted[count]]; //redeclares character

        }

        cout << "Encrypted message is: \n" << messageEncrypted << endl; //prints out encrypted message
        cout << "\nRun again [y/n] ";
        cin >> YorN;

    } while (YorN == 'y'||YorN=='Y');
    return 0;
}

But if I want to loop through again, I can't get the cipher to encrypt a second string.

问题是在您阅读了用户的回复后:

cin >> YorN;

换行符仍然留在输入流中。下一次调用 getline() 只是读取换行符。添加一行以忽略该行的其余部分:

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

紧接着

cin >> YorN;