Shiroe
Shiroe

Reputation: 1

Encryption and Decryption Generator Project

I am mainly doing a school project and I encountered problems:

  1. when ever i went the types of cypher codes like pressing 1 in the main menu where it transports you to the caesar cipher then pressing exit which is 3 then going back to the main menu, press 9 and it gives you thumbs up, after the thumbs up showing up it should end the program but what happens is that instead it waits for another input or something then brings you back to the main menu.

  2. The yes or no option after encrypting a text. after encrypting the test it would give you an option to encrypt or decrypt again. if the answer is y then it would clearscreen then run it all over again and if answer is no then it would exit. Sadly it doesnt work as I thought on how it should be. the thing that happened was that it kept repeating clearscreeen and cant continue to encrypt or exit.

  3. decryption code, i am kind of confused on how to decrypt back a word that was encrypted.

Here is the entire code

//================================================//
//                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                 //
//================================================//

Upvotes: 0

Views: 417

Answers (2)

Apoorva Raju
Apoorva Raju

Reputation: 300

I went through the code I found following solution for your problems

  1. you can check regarding recursive call for menu() function as Yunnosch mentioned . Also you can call exit(EXIT_SUCCESS); when option 9 is selected so the process would succeed with success

  2. cout<< "\n\t Another Encryption (y/n): "; cin>>answer; // either declare answer as character or use 1/0 instead of y/n

3.Decrypt function should be reverse of encrypt . if you shift by 4 character forward in encrypt that should be 4 character backward in decrypt...in your code both case you shift forward.

FYI Edited code

//================================================//
//                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                 //
//================================================//

Upvotes: 1

Yunnosch
Yunnosch

Reputation: 26703

You are calling menu() recursively, that is causing the observed misbehaviour.
If you are inside any menu, apart from the root one inside main() then entering "9" for exit just gets you into one shallower level of recursive menu.
That matches your observed misbehaviour.

To fix just make sure that you only every call menu() from inside main().
All other situations of "I want the menu again", have to be covered by leaving whatever subfunction and returning to the do {} while inside the single menu from main().
That way, any "9" will get you out of that single layer of menu loop, out of the function menu() (called directly from main() and nowhere else) and hence to the end of the program.

Upvotes: 0

Related Questions