简单的Cypher程序无法正确重置
Simple Cypher Program Not Resetting Correctly
我正在学习编程和C 作为我的第一语言。我正在尝试创建一个简单的程序来加密和解密输入,以便我可以使用字符串练习。
一切正常,但是以某种方式我第二次运行程序时会弹出旧输入。
例如,如果我输入" greg",然后解密它,然后再次输入格雷格,我会得到两个" greg"(加密( - 因此,它无法正确重置,它将不重置新单词不重置。
#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;
}
请注意,这并不总是正确的答案,有绩效的影响,但是您必须做的手动书籍越少越好,越好。仅当您无法遇到性能目标并使其复杂化的代码复杂化,而仅使分析证明是一个重大问题所证明的代码复杂。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 简单的Cypher程序无法正确重置