我主要在做一个学校项目,遇到了问题:
每当我进入密码代码类型时,例如在主菜单中按1,它将您带到凯撒密码,然后按exit(退出)3,然后返回主菜单,按9,它会给您竖起大拇指,在竖起大拇指之后,它应该结束程序,但是发生的是,它等待另一个输入或某些东西,然后将您带回主菜单。
加密文本后的是或否选项。加密测试后,它将为您提供再次加密或解密的选项。如果答案为y,则它将清除屏幕,然后重新运行,如果答案为否,则将退出。可悲的是,它不起作用,因为我认为应该如何。发生的事情是它不断重复clearscreeen,无法继续加密或退出。
这是完整的代码
//================================================//
// START OF PROJECT //
//================================================//
//================================================//
// LIBRARY //
//================================================//
#include<iostream>
#include<string>
using namespace std;
//================================================//
// GLOBAL VARIABLES //
//================================================//
//================================================//
// GLOBAL FUNCTIONS //
//================================================//
void intro(); //need more design
int menu(); //function for main menu
void thumbs(); //function for the thumbs up
void caesar(); //function for the caesar cipher
void xo(); //function for the XOR cipher
void hill(); //function for the HILL cipher
void null(); //function for the NULL cipher
void vig(); //function for the VIGENERE cipher
void bacon(); //function for the Baconian cipher
void rot(); //function for the ROT 13 cipher
void keyword(); //function for the keyword cipher
int caesarIN1(); //function within caesar cipher(Encryption)
int caesarIN2(); //function within caesar cipher(Decryption)
string encrypt(); //function on how caesar's encryption
string decrypt(); //function on how caesar's decryption
//************************************************//
// MAIN FUNCTION OF THE SYSTEM //
//************************************************//
int main (){
intro();
menu();
return 0;
}
//************************************************//
// INTRODUCTION //
//************************************************//
void intro(){
cout<<"=======================================";
cout<<"\n\n\t ENCRYPTING ";
cout<<"\n\n\t DECRYPTING";
cout<<"\n\n\t CODE GENERATOR ";
cout<<"\n\n=======================================";
cin.get();
}
//************************************************//
// MAIN MENU FUNCTION //
//************************************************//
int menu(){
int choice;
do{
system("cls");
cout<<"\n\n\n\tMAIN MENU:";
cout<<"\n\n\t01. CAESAR CIPHER";
cout<<"\n\n\t02. XOR CIPHER";
cout<<"\n\n\t03. HILL CIPHER";
cout<<"\n\n\t04. NULL CIPHER";
cout<<"\n\n\t05. VIGENERE CIPHER";
cout<<"\n\n\t06. BACONIAN CIPHER";
cout<<"\n\n\t07. ROT 13 CIPHER";
cout<<"\n\n\t08. KEYWORD CIPHER";
cout<<"\n\n\t09. EXIT SYSTEM";
cout<<"\n\n\tSelect Your Option(1-9): ";
cin>>choice;
system("cls");
switch(choice)
{
case 1:
caesar();
break;
case 2:
xo();
break;
case 3:
hill();
break;
case 4:
null();
break;
case 5:
vig();
break;
case 6:
bacon();
break;
case 7:
rot();
break;
case 8:
keyword();
break;
case 9:
cout<<"Thanks for using the system\n\n";
thumbs();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
cin.ignore();
cin.get();
menu();
break;
}
cin.ignore();
cin.get();
}while(choice!=9);
}
//================================================//
// FUNCTION OF THE SYSTEM //
//================================================//
//************************************************//
// FUNCTION OF CAESAR CIPHER //
//************************************************//
void caesar(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
caesarIN1();
break;
case 2:
caesarIN2();
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION WITHIN CAESAR CIPHER //
//************************************************//
//Encryption
int caesarIN1(){
int answer;
do{
system("cls");
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
string encrypt(string text, int s);
string text;
int s=4, y=0;
cout <<"\n\t ENCRYPTION";
cin.ignore();
cout << "\n\t Text : ";getline(cin,text);
cout << "\t Shift: " << s;
cout << "\n\t Cipher: " << encrypt(text, s);
cout<< "\n\t Another Encryption (y/n): ";
cin>>answer;
}while(answer != 1);
}
//Decryption
int caesarIN2(){
int answer;
do{
system("cls");
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
string decrypt(string text, int s);
string text;
int s=4, y=0, n=1;
cout <<"\n\t DECRYPTION";
cin.ignore();
cout << "\n\t Text : ";getline(cin,text);
cout << "\t Shift: " << s;
cout << "\n\t Cipher: " << decrypt(text, s);
cout<< "\n\t Another Decryption (y/n): ";
cin>>answer;
}while(answer != 1);
}
//************************************************//
// FUNCTION OF XOR CIPHER //
//************************************************//
void xo(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= XOR CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF HILL CIPHER //
//************************************************//
void hill(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= HILL CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF NULL CIPHER //
//************************************************//
void null(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= NULL CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF VIGENERE CIPHER //
//************************************************//
void vig(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= VIGENERE CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF BACONIAN CIPHER //
//************************************************//
void bacon(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= BACONIAN CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF ROT 13 CIPHER //
//************************************************//
void rot(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t== ROT 13 CIPHER ==";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF KEYWORD CIPHER //
//************************************************//
void keyword(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= KEYWORD CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.ignore();
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION FOR THUMBS UP //
//************************************************//
void thumbs(){
cout<<"\t ¶¶¶¶"<<endl;
cout<<"\t ¶ ¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶ ¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶¶¶¶¶¶¶¶¶"<<endl;
}
//================================================//
// CIPHER PROCESS OF EACH FUNCTION //
//================================================//
//************************************************//
// CAESAR CIPHER SYSTEM //
//************************************************//
//ENCRYPTION PROCESS
string encrypt(string text, int s) {
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);
// Encrypt Lowercase letters
else
result += char(int(text[i]+s-97)%26 +97);
}
// Return the resulting string
return result;
}
//DECRYPTION
string decrypt(string text, int s) {
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Decrypt whitespace letters
if (isspace(text[i]))
result += char(int(text[i]));
// Decrypt Uppercase letters
else if (isupper(text[i]))
result += char((int(text[i]+s-65)%26 +65);
// Decrypt Lowercase letters
else
result += char((int(text[i]+s-97)%26 +97);
}
// Return the resulting string
return result;
}
//================================================//
// END OF PROJECT //
//================================================//
答案 0 :(得分:1)
我检查了以下问题的解决方法的代码
您可以检查Yunnosch提到的menu()函数的递归调用。您也可以调用exit(EXIT_SUCCESS);当选择选项9时,该过程将成功完成
cout <<“ \ n \ t另一个加密(y / n):”; cin >> answer; //将答案声明为字符或使用1/0代替y / n
3.Decrypt函数应与crypto反向。如果您在加密中向前移动了4个字符,而在解密中应该向后移动了4个字符...在您的代码中,两种情况都将向前移动。
FYI编辑代码
//================================================//
// START OF PROJECT //
//================================================//
//================================================//
// LIBRARY //
//================================================//
#include<iostream>
#include<string>
using namespace std;
//================================================//
// GLOBAL VARIABLES //
//================================================//
//================================================//
// GLOBAL FUNCTIONS //
//================================================//
void intro(); //need more design
int menu(); //function for main menu
void thumbs(); //function for the thumbs up
void caesar(); //function for the caesar cipher
void xo(); //function for the XOR cipher
void hill(); //function for the HILL cipher
void null(); //function for the NULL cipher
void vig(); //function for the VIGENERE cipher
void bacon(); //function for the Baconian cipher
void rot(); //function for the ROT 13 cipher
void keyword(); //function for the keyword cipher
int caesarIN1(); //function within caesar cipher(Encryption)
int caesarIN2(); //function within caesar cipher(Decryption)
string encrypt(); //function on how caesar's encryption
string decrypt(); //function on how caesar's decryption
//************************************************//
// MAIN FUNCTION OF THE SYSTEM //
//************************************************//
int main (){
intro();
menu();
return 0;
}
//************************************************//
// INTRODUCTION //
//************************************************//
void intro(){
cout<<"=======================================";
cout<<"\n\n\t ENCRYPTING ";
cout<<"\n\n\t DECRYPTING";
cout<<"\n\n\t CODE GENERATOR ";
cout<<"\n\n=======================================";
cin.get();
}
//************************************************//
// MAIN MENU FUNCTION //
//************************************************//
int menu(){
int choice;
do{
system("cls");
cout<<"\n\n\n\tMAIN MENU:";
cout<<"\n\n\t01. CAESAR CIPHER";
cout<<"\n\n\t02. XOR CIPHER";
cout<<"\n\n\t03. HILL CIPHER";
cout<<"\n\n\t04. NULL CIPHER";
cout<<"\n\n\t05. VIGENERE CIPHER";
cout<<"\n\n\t06. BACONIAN CIPHER";
cout<<"\n\n\t07. ROT 13 CIPHER";
cout<<"\n\n\t08. KEYWORD CIPHER";
cout<<"\n\n\t09. EXIT SYSTEM";
cout<<"\n\n\tSelect Your Option(1-9): ";
cin>>choice;
system("cls");
switch(choice)
{
case 1:
caesar();
break;
case 2:
xo();
break;
case 3:
hill();
break;
case 4:
null();
break;
case 5:
vig();
break;
case 6:
bacon();
break;
case 7:
rot();
break;
case 8:
keyword();
break;
case 9:
cout<<"Thanks for using the system\n\n";
thumbs();
exit(0);
default :
cout<<"INVALID INPUT\n";
system("pause");
cin.ignore();
cin.get();
menu();
break;
}
}while(choice!=9);
return 0;
}
//================================================//
// FUNCTION OF THE SYSTEM //
//================================================//
//************************************************//
// FUNCTION OF CAESAR CIPHER //
//************************************************//
void caesar(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
caesarIN1();
break;
case 2:
caesarIN2();
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION WITHIN CAESAR CIPHER //
//************************************************//
//Encryption
int caesarIN1(){
int answer;
do{
system("cls");
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
string encrypt(string text, int s);
string text;
int s=4, y=0;
cout <<"\n\t ENCRYPTION";
cin.ignore();
cout << "\n\t Text : ";getline(cin,text);
cout << "\t Shift: " << s;
cout << "\n\t Cipher: " << encrypt(text, s);
cout<< "\n\t Another Encryption (y/n): ";
cin>>answer; // either use 1/0 or make answer as char
}while(answer != 1);
return 0;
}
//Decryption
int caesarIN2(){
int answer;
do{
system("cls");
cout<<"\n\t==============================";
cout<<"\n\t= CAESAR CIPHER =";
cout<<"\n\t==============================";
string decrypt(string text, int s);
string text;
int s=4, y=0, n=1;
cout <<"\n\t DECRYPTION";
cin.ignore();
cout << "\n\t Text : ";getline(cin,text);
cout << "\t Shift: " << s;
cout << "\n\t Cipher: " << decrypt(text, s);
cout<< "\n\t Another Decryption (y/n): ";
cin>>answer;
}while(answer != 1);
return 0;
}
//************************************************//
// FUNCTION OF XOR CIPHER //
//************************************************//
void xo(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= XOR CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION OF HILL CIPHER //
//************************************************//
void hill(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= HILL CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION OF NULL CIPHER //
//************************************************//
void null(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= NULL CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
cin.get();
}while(choice!=3);
}
//************************************************//
// FUNCTION OF VIGENERE CIPHER //
//************************************************//
void vig(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= VIGENERE CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION OF BACONIAN CIPHER //
//************************************************//
void bacon(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= BACONIAN CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION OF ROT 13 CIPHER //
//************************************************//
void rot(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t== ROT 13 CIPHER ==";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION OF KEYWORD CIPHER //
//************************************************//
void keyword(){
int choice;
do{
cout<<"\n\t==============================";
cout<<"\n\t= KEYWORD CIPHER =";
cout<<"\n\t==============================";
cout<<"\n\n\tCHOOSE ACTION:";
cout<<"\n\n\t01. ENCRYPT";
cout<<"\n\n\t02. DECRYPT";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tSelect Your Option(1-3): ";
cin>>choice;
switch(choice)
{
case 1:
break;
case 2:
break;
case 3:
menu();
break;
default :
cout<<"INVALID INPUT\n";
system("pause");
caesar();
}
}while(choice!=3);
}
//************************************************//
// FUNCTION FOR THUMBS UP //
//************************************************//
void thumbs(){
cout<<"\t ¶¶¶¶"<<endl;
cout<<"\t ¶ ¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶ ¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶¶¶¶¶¶¶¶¶¶¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶ ¶"<<endl;
cout<<"\t ¶¶¶¶¶¶¶¶¶¶"<<endl;
}
//================================================//
// CIPHER PROCESS OF EACH FUNCTION //
//================================================//
//************************************************//
// CAESAR CIPHER SYSTEM //
//************************************************//
//ENCRYPTION PROCESS
string encrypt(string text, int s) {
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);
// Encrypt Lowercase letters
else
result += char(int(text[i]+s-97)%26 +97);
}
// Return the resulting string
return result;
}
//DECRYPTION
string decrypt(string text, int s) {
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Decrypt whitespace letters
if (isspace(text[i]))
result += char(int(text[i]));
// Decrypt Uppercase letters
else if (isupper(text[i]))
result += char((int(text[i] - s-65)%26 +65));
// Decrypt Lowercase letters
else
result += char((int(text[i] - s-97)%26 +97));
}
// Return the resulting string
return result;
}
//================================================//
// END OF PROJECT //
//================================================//
答案 1 :(得分:0)
您正在递归调用menu()
,这导致观察到的异常行为。
如果您在任何菜单中,除了main()
内的根目录之外,然后输入“ 9”退出,只会使您进入较浅级别的递归菜单。
这与您观察到的不良行为相符。
要解决此问题,只需确保您只有menu()
内部的每个呼叫main()
。
必须通过保留任何子功能并从do {} while
返回单个菜单中的main()
来覆盖“我再次想要菜单”的所有其他情况。
这样,任何“ 9”将使您退出菜单循环的这一层,退出功能menu()
(直接从main()
调用,无其他地方),从而到达程序结尾。