Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by 100701712 ( 7 years ago )
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void caesarCipher();

int main() {
    caesarCipher();
    return 0;
}

void caesarCipher() { //Question 2
    int iKey = 5;
    string sSelection; //String Used for Exception Handling
    bool bRun = true;

    cout << "Welcome to the ENGR1200U Cryptographic Techniques Program";

    while (bRun) {
        cout << "\n\nPlease enter your selection: \n";
        cout << "   1. Encrypt\n   2. Decrypt\n";
        cin >> sSelection;

        if (sSelection.substr(0) == "1") { //Encrypt
            string sInput, sResult = "";

            cout << "\nPlease enter a message to encrypt: ";
            cin.ignore();
            getline(cin, sInput);

            transform(sInput.begin(), sInput.end(), sInput.begin(), toupper);

            cout << "\nYour encrypted message is: ";

            for (int i = 0; i < sInput.length(); i++) {
                if (ispunct(sInput[i])) { //Punctuation Handling
                    sResult += sInput[i];
                }

                else if (sInput[i] == ' ') { //Space Handling
                    sResult += ' ';
                }

                else {
                    sResult += char(int(sInput[i] - 65 + iKey) % 26 + 65);
                }
            }

            cout << sResult;

            string sContinue;

            while (1 > 0) {
                cout << "\n\nWould you like to continue (Y / N)?: ";
                cin >> sContinue;

                if (sContinue.substr(0) == "y" || sContinue.substr(0) == "Y") {
                    break;
                }

                else if (sContinue.substr(0) == "n" || sContinue.substr(0) == "N") {
                    bRun = false;
                    break;
                }

                else {
                    cout << "\nError: Please enter a valid selection (Y / N).\n";
                }
            }
        }

        else if (sSelection.substr(0) == "2") { //Decrypt
            string sInput, sResult = "";

            cout << "\nPlease enter a message to decrypt: ";
            cin.ignore();
            getline(cin, sInput);

            transform(sInput.begin(), sInput.end(), sInput.begin(), toupper);

            cout << "\nYour decrypted message is: ";

            for (int i = 0; i < sInput.length(); i++) {
                if (ispunct(sInput[i])) { //Punctuation Handling
                    sResult += sInput[i];
                }

                else if (sInput[i] == ' ') { //Space Handling
                    sResult += ' ';
                }

                else {
                    sResult += char(int(sInput[i] + 65 - iKey) % 26 + 65);
                }
            }

            cout << sResult;

            string sContinue;

            while (1 > 0) {
                cout << "\n\nWould you like to continue (Y / N)?: ";
                cin >> sContinue;

                if (sContinue.substr(0) == "y" || sContinue.substr(0) == "Y") {
                    break;
                }

                else if (sContinue.substr(0) == "n" || sContinue.substr(0) == "N") {
                    bRun = false;
                    break;
                }

                else {
                    cout << "\nError: Please enter a valid selection (Y / N).\n";
                }
            }
        }

        else {
            cout << "\n\nError: Please enter a valid selection (1 / 2).\n";
        }
    }
}

 

Revise this Paste

Your Name: Code Language: