简单密码程序未正确重置 我学习编程和C++作为我的第一个语言来娱乐。我正在尝试创建一个简单的程序来加密和解密输入,这样我就可以练习使用字符串了

简单密码程序未正确重置 我学习编程和C++作为我的第一个语言来娱乐。我正在尝试创建一个简单的程序来加密和解密输入,这样我就可以练习使用字符串了,c++,c++11,C++,C++11,一切正常,但不知何故,当我再次运行程序时,旧的输入会弹出 例如,如果我输入“greg”,然后解密它,然后再次输入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

一切正常,但不知何故,当我再次运行程序时,旧的输入会弹出

例如,如果我输入“greg”,然后解密它,然后再次输入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;
}
#包括
#包括
使用名称空间std;
int main(){
字符串字母表{“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"};
字符串键{“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"};
字符串{};
字符串secretword{};
字符串解密字{};
int选择{};
//int newpos{};
int位置{};
//int-keypos{};
做{

cout程序对加密输出和解密输入都使用
secretword
,并且从不清除。Ooops

与其清除变量,不如缩小它们的范围,这样它们就不需要被清除,也不会发生冲突。例如:

#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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串字母表{“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" };
字符串键{“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选择;
做
{

cout程序对加密输出和解密输入都使用
secretword
,并且从不清除。Ooops

与其清除变量,不如缩小它们的范围,这样它们就不需要被清除,也不会发生冲突。例如:

#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;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串字母表{“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" };
字符串键{“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选择;
做
{
库特