Skip to content

Commit

Permalink
much better input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
calebTree committed Aug 19, 2021
1 parent 51bca63 commit f8704ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
35 changes: 17 additions & 18 deletions caesar-cipher.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#include "caesar-cipher.h"
#include <iostream>

// https://www.tutorialspoint.com/cplusplus-program-to-implement-caesar-cypher
// https://www.asciihex.com/

using namespace std;

void encryption(const string &plainAlpha, char &discard) {
void encryption(const string &plainAlpha) {
int key;
string message, cipherAlphabet, cipherText;

// input number of places to shift left
cout << "Enter your chosen cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
while (!cin || key > 25 || key < 1) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
cin.get(discard); // discard \n
cout << "Enter the message to encrypt. (lowercase, roman alpha): "; // input message to encrypt
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard \n
cout << "Enter the message to encrypt: "; // input message to encrypt
getline(cin, message);

// shift alphabet [key] letters to the left
Expand All @@ -35,21 +34,21 @@ void encryption(const string &plainAlpha, char &discard) {
cout << "Ciphertext: " << cipherText << endl << endl;
}

void decryption(const string &plainAlpha, char &discard) {
void decryption(const string &plainAlpha) {
int key;
string cipherText;

cout << "Enter the known cipher key. (integer, 1-25): ";
cout << "Enter your known cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
while (!cin || key > 25 || key < 1) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
cin.get(discard); // discard \n
cout << "Enter the message to decrypt. (lowercase, roman alpha): "; // input message to decrypt
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // discard \n
cout << "Enter the message to decrypt: "; // input message to decrypt
getline(cin, cipherText);

cout << "\nCiphertext: " << cipherText << endl;
Expand Down
14 changes: 7 additions & 7 deletions caesar-cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
#endif //CAESAR_CIPHER_CAESAR_CIPHER_H

#include <iostream>
#include<limits>

using namespace std;

void encryption(const string&, char&);
void encryption(const string&);
// Function to display encrypt interface
// Postcondition: string is the plain alphabet displayed and ciphered;
// char discards \n
// Postcondition: const string is the alphabet displayed in plain then in ciphered

void decryption(const string&, char&);
void decryption(const string&);
// Function to display decrypt interface
// Postcondition: string is the plain alphabet displayed and ciphered;
// char discards \n
// Postcondition: const string is the alphabet displayed in plain then in ciphered

string cipherAlpha(int&, const string&);
// Function to generate cipher alphabet.
// Postcondition: const string shifted left by int positions
// Postcondition: const string is shifted left by const int positions

string cipherMessage(const string&, int&);
// Function to encrypt the message
Expand Down
24 changes: 15 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,34 @@ using namespace std;
int main() {
cout << "\nWelcome to Julius Caesar's Shift Cipher!"
"\nEnter an integer to shift the normal alphabet left by that number of positions." << endl;
cout << "Then, enter a message that will be encrypted using your cipher alphabet." << endl;
cout << "Then, enter a message that will be encrypted using the generated cipher alphabet." << endl;
cout << "https://en.wikipedia.org/wiki/Caesar_cipher\n" << endl;

int option = -1;
int option;
string plainAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char discard;

while (option != 0) {
while (cin) {
cout << "(1) Encrypt, (2) Decrypt, (0) Quit: ";
cin >> option;
if (!cin) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Error! Invalid input." << endl;
continue;
}
switch (option) {
case 0:
break;
return 0;
case 1:
encryption(plainAlpha, discard);
encryption(plainAlpha);
break;
case 2:
decryption(plainAlpha, discard);
decryption(plainAlpha);
break;
default:
cout << "Invalid choice." << endl;
cout << "Error! Invalid choice." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
return 0;
}

0 comments on commit f8704ad

Please sign in to comment.