k; } 库特,c++,C++" /> k; } 库特,c++,C++" />

为什么';while循环是否再次获取输入字符串? 我为C++中的CaleSyfter编写了以下代码。我在这里使用while循环在这里输入code,因为当用户加密/解密消息时,应该有一个循环用于再次尝试。但是当我运行代码时,对于第一次迭代,它工作得非常好,但是当它第二次运行时,它不接受输入字符串。 这是我的密码 #include <conio.h> #include <stdio.h> #include <iostream> using namespace std; int main() { char x = 'y'; int k, option; string msg; string encrypt; string decrypt; while (x != 'n') // run until user selects n { cout << "Please enter your message: "; getline(cin, msg); // gets input message cout << "\nEnter number for the key between 1-25: "; cin >> k; while (k < 1 || k > 25) { cout << "\nPlease select valid key between 1 and 25 "; cin >> k; } cout << "\nSelect option: " << "\n1 for Encrypt " << "\n2 for Decrypt " << "\n"; cin >> option; while (option != 1 && option != 2) { cout << "\nSorry wrong option. Please select again\n "; cin >> option; } switch (option) { case 1: // for encryption for (int i = 0; i < msg.length(); i++) { if (isalpha(msg[i])) { if (islower(msg[i])) { encrypt[i] = (((msg[i] - 97) + k) % 26) + 97; cout << encrypt[i]; } } if (isupper(msg[i])) { encrypt[i] = (((msg[i] - 65) + k) % 26) + 65; cout << encrypt[i]; } } break; case 2: // for decryption for (int i = 0; i < msg.length(); i++) { if (islower(msg[i])) { decrypt[i] = ((((msg[i] - 97) - k) + 26) % 26) + 97; cout << decrypt[i]; } if (isupper(msg[i])) { decrypt[i] = ((((msg[i] - 97) - k) + 26) % 26) + 97; cout << decrypt[i]; } } break; } cout << "\nDo you want to try again?(Y/N) "; // asking user if he wants to // encrypt or decrypt again cin >> x; } } #包括 #包括 #包括 使用名称空间std; int main(){ char x='y'; int k,选项; 串味精; 字符串加密; 字符串解密; while(x!=“n”)//运行直到用户选择n { 库特k; 而(k25){ cout>k; } 库特

为什么';while循环是否再次获取输入字符串? 我为C++中的CaleSyfter编写了以下代码。我在这里使用while循环在这里输入code,因为当用户加密/解密消息时,应该有一个循环用于再次尝试。但是当我运行代码时,对于第一次迭代,它工作得非常好,但是当它第二次运行时,它不接受输入字符串。 这是我的密码 #include <conio.h> #include <stdio.h> #include <iostream> using namespace std; int main() { char x = 'y'; int k, option; string msg; string encrypt; string decrypt; while (x != 'n') // run until user selects n { cout << "Please enter your message: "; getline(cin, msg); // gets input message cout << "\nEnter number for the key between 1-25: "; cin >> k; while (k < 1 || k > 25) { cout << "\nPlease select valid key between 1 and 25 "; cin >> k; } cout << "\nSelect option: " << "\n1 for Encrypt " << "\n2 for Decrypt " << "\n"; cin >> option; while (option != 1 && option != 2) { cout << "\nSorry wrong option. Please select again\n "; cin >> option; } switch (option) { case 1: // for encryption for (int i = 0; i < msg.length(); i++) { if (isalpha(msg[i])) { if (islower(msg[i])) { encrypt[i] = (((msg[i] - 97) + k) % 26) + 97; cout << encrypt[i]; } } if (isupper(msg[i])) { encrypt[i] = (((msg[i] - 65) + k) % 26) + 65; cout << encrypt[i]; } } break; case 2: // for decryption for (int i = 0; i < msg.length(); i++) { if (islower(msg[i])) { decrypt[i] = ((((msg[i] - 97) - k) + 26) % 26) + 97; cout << decrypt[i]; } if (isupper(msg[i])) { decrypt[i] = ((((msg[i] - 97) - k) + 26) % 26) + 97; cout << decrypt[i]; } } break; } cout << "\nDo you want to try again?(Y/N) "; // asking user if he wants to // encrypt or decrypt again cin >> x; } } #包括 #包括 #包括 使用名称空间std; int main(){ char x='y'; int k,选项; 串味精; 字符串加密; 字符串解密; while(x!=“n”)//运行直到用户选择n { 库特k; 而(k25){ cout>k; } 库特,c++,C++,此外,您不能处理未初始化的字符串 encrypt[i]=…到encrypt+=… decrypt[i]=…到decrypt+=… secound decrypt是重复的,请将其更改为: decrypt += ((((msg[i] - 65) - k) + 26) % 26) + 65; 您需要在cin>>x之后立即刷新代码末尾的缓冲区。这是一种方法 ... cin.clear(); cin.ignore(nu

此外,您不能处理未初始化的字符串

encrypt[i]=…
encrypt+=…

decrypt[i]=…
decrypt+=…

secound decrypt是重复的,请将其更改为:

decrypt += ((((msg[i] - 65) - k) + 26) % 26) + 65;

您需要在
cin>>x
之后立即刷新代码末尾的缓冲区。这是一种方法

...                                         

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');`
。。。
cin.clear();
cin.ignore(数值限制::max(),'\n')`
也不要忘了包括数值限制的
\include

这是你可能会觉得有用的帖子


更新:正如我在注释部分所更正的那样,
cin.clear();
仅在错误输入在流中设置了错误标志时才是必需的。

在此语句getline(cin,msg)之前;使用std::cin.ignore(std::numeric\u limits::max(),“\n”);您需要包含标题限制fyi,需要包含
。仅此而已。您还有未定义的行为,因为您正在访问
加密
解密
。这两个都是空的,您需要为它们编制索引。您需要
。向后推
(或
+=
)在它们上或
。首先将它们调整为所需大小。在
cin>>x之后,在末尾尝试添加
cin.ignore()
刷新换行符。建议:在测试前编写更少的代码。写出几百行代码,然后发现由于前几行中的错误,所有或大部分工作都变成了垃圾,这很糟糕。如果第一步是从用户获取数据,请编写并测试用户输入代码。不要浪费时间在知道输入正确之前,不要刷新任何内容。术语说明:这不是刷新。这只是删除选定的数据。
cin.clear()
只有在错误输入在流中设置了错误标志时才有必要。@user4581301注意到了。你是对的!谢谢你的帮助。它解决了我的问题。我面临的另一个问题是,当我输入一个有空格的字符串并试图加密它时,加密的消息中没有空格。虽然我认为加密/解密的消息消息也有原来字符串中的空格。你能帮我一下吗?旁注:在
'a'
更合适的地方不要使用65。它通常使代码更容易阅读,并且更便于移植。
decrypt += ((((msg[i] - 65) - k) + 26) % 26) + 65;
...                                         

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');`