c++ - 简单的密码程序未正确重置
问题描述
我正在学习编程和 c++ 作为我的第一语言来娱乐。我正在尝试创建一个简单的程序来加密和解密输入,以便我可以练习字符串。
一切正常,但不知何故,当我再次运行该程序时,我最终会弹出旧输入。
例如,如果我输入 "greg" ,然后解密,然后再次输入 greg,我得到两个 "greg"(encrypted)- 所以它没有正确重置,它会继续添加新单词而不重置。
#include <iostream>
#include <string>
using namespace std;
int main() {
string alphabet{"a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 "
"5 6 7 8 9 0"};
string key{"m h i p g f b t x u r l w a j e q k z d y v o c n s 9 5 0 7 8 2 "
"3 4 1 6"};
string word{};
string secretword{};
string decryptedword{};
int selection{};
// int newpos {};
int position{};
//int keypos{};
do {
cout << "-------------------------------------" << endl;
cout << "Select an Option: " << endl;
cout << "1: Encrypt" << endl;
cout << "2: Decrypt" << endl;
cout << "3: Quit" << endl;
cin >> selection;
if (selection == 1) {
cout << "Enter a Word to Encrypt:";
cin.sync();
getline(cin, word);
for (auto i : word) {
if (isupper(i)) {
cout << "Please use lower case only" << endl;
break;
}
position = alphabet.find(i);
secretword += key.at(position);
}
cout << "Encrypted Word: " << secretword << endl;
secretword = "";
}
if (selection == 2) {
cout << "Enter a Word to Decrypt: ";
cin.sync();
getline(cin, secretword);
for (auto i : secretword) {
if (isupper(i)) {
cout << "Please use lower case only" << endl;
break;
}
position = key.find(i);
decryptedword += alphabet.at(position);
}
cout << "Decrypted Word: " << decryptedword << endl;
decryptedword = "";
}
} while (selection != 3);
return 0;
}
解决方案
该程序secretword
用于加密的输出和解密的输入,并且从不清除它。哎呀。
与其清除变量,不如缩小它们的范围,这样它们就不需要被清除并且不会发生冲突。例如:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string alphabet { "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 "
"5 6 7 8 9 0" };
string key { "m h i p g f b t x u r l w a j e q k z d y v o c n s 9 5 0 7 8 2 "
"3 4 1 6" };
//int keypos{};
int selection;
do
{
cout << "-------------------------------------" << endl;
cout << "Select an Option: " << endl;
cout << "1: Encrypt" << endl;
cout << "2: Decrypt" << endl;
cout << "3: Quit" << endl;
cin >> selection;
if (selection == 1)
{
cout << "Enter a Word to Encrypt:";
cin.sync();
string word;
string secretword;
getline(cin, word);
for (auto i : word)
{
if (isupper(i))
{
cout << "Please use lower case only" << endl;
break;
}
auto position = alphabet.find(i);
secretword += key.at(position);
}
cout << "Encrypted Word: " << secretword << endl;
secretword = "";
}
if (selection == 2)
{
cout << "Enter a Word to Decrypt: ";
cin.sync();
string secretword;
string decryptedword;
getline(cin, secretword);
for (auto i : secretword)
{
if (isupper(i))
{
cout << "Please use lower case only" << endl;
break;
}
auto position = key.find(i);
decryptedword += alphabet.at(position);
}
cout << "Decrypted Word: " << decryptedword << endl;
decryptedword = "";
}
} while (selection != 3);
return 0;
}
请注意,这并不总是正确的答案,这会影响性能,但是您必须做的手动簿记越少越好。仅当您无法达到性能目标时才使代码复杂化,并且仅使通过分析证明是一个重大问题的代码复杂化。
推荐阅读
- azure - Azure 数据工厂 - 将数据导出到子容器/blob
- javascript - 在 Javascript 中,ƒ 是什么意思?
- php - 带回复的 PHP MySQL 评论系统
- r - 在使用 R 时,在 Wilcox 符号秩检验期间发现 V 为 0 时,如果有的话,这意味着什么?
- inno-setup - 在 Inno Setup 中,如何在向导窗口的左下方添加图像横幅?
- r - 为什么在循环 API 调用时出现“替换长度为零”错误?
- sql - 将 2 个表加入 1 个表
- laravel - 在数据透视表 laravel 中添加与附加值的关系
- linux - “git push -u origin --all”仅在 AWS 虚拟 Linux 实例中失败 - 可能与 ssh 相关
- python - 如何在numpy中返回图像中区域的坐标及其尺寸?