I have write a program and suddenly this is ignoring getline(cin, userInput); and executing all the functions in the assigned choice section. For example: If I want to encrypt the message, I will have to press E in the choice menu, and when I press E the program executes all the materials in the encryption function without asking me for the message that I want to be encrypted. But working fine with just cin >> userInput . What can be cause of this issue or maybe I am missing something to add.
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
using namespace std;
void MainMenu();
void Decision();
void encryption ();
void decryption ();
void ExitProgram ();
int main()
{
MainMenu();
return 0;
}
void MainMenu()
{
cout << "Enter any option that you want to perform" << '\n';
cout << "=========================================" << '\n';
cout << "E for encryption" << '\n';
cout << "D for decryption" << '\n';
cout << "Q to quite the program" << '\n';
cout << "========================================" << '\n';
cout << "Choice: ";
Decision();
}
void Decision()
{
char Choice{};
cin >> Choice;
switch (Choice)
{
case 'E': case 'e': encryption ();break;
case 'D': case 'd': decryption (); break;
case 'Q': case 'q': ExitProgram(); break;
default:
cout << "You have make a wrong decision! try again: ";
cin >> Choice;
}
}
void encryption ()
{
string alphabets {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string userInput {};
string encryptedMessage {};
cout << "Enter the message you want to be Encrypted: ";
getline(cin, userInput);
cout << "Encrypting your message, please wait";
chrono::duration<int, std::milli> timespan(1000);
this_thread::sleep_for(timespan);
cout << ".";
this_thread::sleep_for(timespan);
cout << ".";
this_thread::sleep_for(timespan);
cout << "." << '\n';
this_thread::sleep_for(timespan);
for (char c:userInput)
{
size_t pos_c = {alphabets.find(c)};
if(pos_c != string::npos)
{
char pos_c_key = key.at(pos_c);
encryptedMessage += pos_c_key;
}else encryptedMessage += c;
}
cout << "The encrypted message is: " << encryptedMessage << '\n';
}
void decryption ()
{
string alphabets {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string key {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string userInput {};
string decryptedMessage {};
cout << "Enter the message you want to be Decrypted: ";
getline(cin, userInput);
cout << "Decrypting your message, please wait";
chrono::duration<int, std::milli> timespan(1000);
this_thread::sleep_for(timespan);
cout << ".";
this_thread::sleep_for(timespan);
cout << ".";
this_thread::sleep_for(timespan);
cout << "." << '\n';
this_thread::sleep_for(timespan);
for (char N:userInput)
{
size_t pos_N = {alphabets.find(N)};
if(pos_N != string::npos)
{
char pos_N_key = key.at(pos_N);
decryptedMessage += pos_N_key;
} else decryptedMessage += N;
}
cout << "The Decrypted message is: " << decryptedMessage << '\n';
}
void ExitProgram ()
{
cout << "Thank you for using the program. Good By" << '\n';
return;
}