结束while循环为字符替换
问题描述:
这是从Keyshanc加密算法取出。 https://github.com/Networc/keyshanc结束while循环为字符替换
我的问题是:如何可能操纵具有与从密码阵列所选择的密钥多重加密输出该主要方法?
我不能就在年底打出来的编码循环。
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;
}
答
您正在两个地方进行输入,因此您必须进行两项测试。
当您在while
循环中时,必须跳出while
循环,并跳出for(;;)
循环。您可以设置i=5;
迫使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;
}
}
}
此外trueChar
和inputChar
应该初始化为0
不NULL
(虽然这是同样的事情在这一点)。如果未初始化,请勿打印trueChar
。
if (trueChar)
cout << trueChar;