对于第一次运行,密码将加密字符串。但是如果我想再次循环,我就无法获得加密第二个字符串的密码。
有没有办法在不使用指针的情况下实现这一目标?
#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;
}
答案 0 :(得分:0)
但是如果我想再次循环,我就无法获得加密第二个字符串的密码。
问题在于,在您使用以下方式阅读用户的回复后:
cin >> YorN;
新行字符仍保留在输入流中。下一次调用getline()
只会读取换行符。添加一行以忽略该行的其余部分:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
后立即
cin >> YorN;