2

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;
}
  • 1
    This question must not be closed since the condition is absolutely different and not a duplicate question. votes up – Tahir Oct 22 '20 at 17:51
  • 1
    @Tahir Yes I also don't get the answer by the mentioned article which is claiming it is duplicated. Still looking for the solution – Indiana Jones Oct 22 '20 at 17:52
  • 4
    The duplicate is valid. This code is using `cin >> ...` to read the user's menu choice, and is then using `getline(cin, ...)` to read subsequent input without first reading a line break from `cin` that may have been left behind by `>>`, thus causing `getline()` to not read the input correctly. The solution is to either 1) use `getline()` to read the menu choice, or 2) to `cin.ignore()` the line break before calling `getline()`. – Remy Lebeau Oct 22 '20 at 17:57
  • @RemyLebeau as I already mention that using in main function the getline(cin, userInput) is not ignored, but in void encryption() is is ignored. Even cin.ignore() is not changing this behavior, it is still ignored. – Indiana Jones Oct 22 '20 at 18:05
  • 2
    Using `cin >> ...` in `Decision()` followed afterwards by `getline(cin, ...)` in `encryption()` etc WITHOUT extracting the line break left behind by `>>` is EXACTLY why the `getline()` in `encryption()` etc is being ignored, just as the duplicate explained. If ignoring that line break is not working, then please [edit] the question to show the actual input that is being typed in. And BTW, `Decision()` is missing a loop, needed to handle the case when the user enters bad input, otherwise the 2nd `cin >> Choice;` is useless. – Remy Lebeau Oct 22 '20 at 18:38
  • @RemyLebeau Good day bro, thank you you solved my problem, I deleted the 2nd useless cin >> ....; and added the cin.ignore(); Right After 1st cin >> ....; and I see it has added a break line to make work getline.(cin, ...); – Indiana Jones Oct 23 '20 at 13:19

0 Answers0