0

I'm new to Coding and C++ is my first language, so I made this program which ciphers and deciphers the text after taking text(string) input from user, I want this program to be able to use multiple times in 1 run, so I made this recursive loop, program runs fine in first iteration , but in 2nd iteration it just skips the text input and moves forward

void cipherMain()
{
    std::string text;
    int selectOpt;

    std::cout << "Enter Text" << std::endl;
    getline(std::cin, text);
    // std::cin >> text;

    std::cout << "1.Cipher the text\n2.Decipher the text\n";
    std::cout << "Select your option: ";
    std::cin >> selectOpt;

    std::string cipheredText;
    std::string deCipheredText;

    switch (selectOpt)
    {
    case 1:
        cipheredText = cipher(text);
        std::cout << "Ciphered text is:\n\"" << cipheredText << "\"\n";
        system("pause");
        break;

    case 2:
        deCipheredText = dCipher(text);
        std::cout << "Deciphered text is:\n\"" << deCipheredText << "\"\n";
        system("pause");
        break;

    default:
        std::cout << "Invalid Input" << std::endl;
        break;
    }

    char restart;
    std::cout << "Enter 'y' to restart the program or 'n' to exit the program\n";
    std::cin >> restart;
    
    if(restart=='y')
    {
        system("cls");
        cipherMain();
    }
    else
    {
        system("exit");
    }
    
}

What I tried: I replaced getline(std::cin,text) with std::cin >> text; this works, but I want my program to take whole line instead of just taking 1 word in input. I want to get my getline(std::cin,text) working

this is 1st iteration

this is my 2nd iteration where text input is ignored

  • Please copy-paste text, instead of providing images of text. – Algirdas Preidžius May 21 '21 at 09:24
  • 1
    This is a slightly obscure case of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). (It happens "across" function calls, which makes it difficult to spot.) – molbdnilo May 21 '21 at 09:26
  • Side notes: don't recurse - use a loop, and don't use `system("exit")` - use `return;`. – molbdnilo May 21 '21 at 09:27
  • those are images when i ran my program, it is just to show how my 2nd iteration is ignoring input and moving forward. – UraniumX92 May 21 '21 at 09:30
  • @molbdnilo can i know reasons for why not to use recursion and `system("exit")` – UraniumX92 May 21 '21 at 09:38
  • A loop is more suitable (it would make your bug more visible, for one), and why would you call an external platform-dependent command when you can just return from the function? (I can't imagine any situation at all where `system("exit")` is appropriate.) – molbdnilo May 21 '21 at 09:43
  • @molbdnilo oh thanks, similarly is there any alternative for `system("cls")` it clears the console screen – UraniumX92 May 21 '21 at 09:55
  • Not clearing the screen is the best approach. – molbdnilo May 21 '21 at 09:58
  • If you recurse, you are also filling the stack up, which can be avoided with the loop that @molbdnilo suggested? – nahzor May 21 '21 at 10:14
  • yes, it did solve my question, thanks to @molbdnilo , forgot to say it earlier that it solved it – UraniumX92 May 21 '21 at 17:45

0 Answers0