0

I have an application that is waiting on input from the user to issue a command, when it receives the command, a large amount is printed to the screen. The app crashes when looping around waiting on std::getline the second time. I think this is because the buffer is full or something. If I hardcode the string rather than reading from std::cin there is no crash. How can I flush the buffer before reading the second time, I used the following but it doesn't work std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

#include <iostream>
#include <string>

int main ()
{
    while(true){
        std::string input{};
        // if string is hardcoded app works so issue is with std::cin
        // std::string input{"command_one"};
        if (!std::getline(std::cin, input))
        {
        continue;
        }
         // app does work and prints alot to screen
    }
}
tester123
  • 399
  • 1
  • 3
  • 10
  • 1
    How are you coming out from while(true) loop? you are running with endless loop here. – Sanjeev Apr 10 '20 at 09:23
  • @Sanjeev The app is an endless loop, there is a lot happening after I issue the command. I am just giving a simple example. – tester123 Apr 10 '20 at 09:25
  • This is nowhere *near* a [mcve]. It's certainly minimal, and i guess it's an example, but the important attribute of reproducing your problem is sorely lacking. What happens when you omit the "lot happening" after you issue the command. And what is the *source* of this input? is it a file redirection or some such? Finally, `if (!std::getline(std::cin, input)) continue;` is madness. Nothing changes the fail-state of the stream, so it will fail again on the next iteration. You *did* fire `std::cin.clear()` prior to your white space consumption effort, right? – WhozCraig Apr 10 '20 at 09:26
  • @WhozCraig the source of the input is `std::cin` I have that in the example. The only difference between the crash happening and not is if I hardcode the command, rather than reading from `std::cin`. And the crash occurs on the second iteration after a lot has been printed to the screen – tester123 Apr 10 '20 at 09:29
  • You didn't understand my question. It there a user typing at a console? Or are you flooding `std::cin` with input from a file redirection to your program's std::cin or otherwise non-user source of input. You also didn't answer my question. If you omit all of the code following `// app does work and prints alot to screen`, does your program *still* crash even with console input from the user rather than hard-coded? I would think not. Update your post to include a [mcve]. Whatever that takes. The more minimal the better, so long as it reproduces the problem. – WhozCraig Apr 10 '20 at 09:31
  • @WhozCraig apologies, yes that is user input typing on the screen. No it doesn't crash, but it is that section of code which spits alot out on std::cout. As I said if I change one single line of code in the whole application to hardcode the command rather than reading from `std::in` there is no issue. I was just wondering is there a flush or something that can be issue. – tester123 Apr 10 '20 at 09:49
  • If that call to `std::getline` fails all subsequent calls to it will also fail, so the program loops infinitely without getting new input. `continue;` is not sufficient when an error occurs; you have to fix the problem. – Pete Becker Apr 10 '20 at 13:30
  • @PeteBecker How do I retrieve the return value from `std::getline`? – tester123 Apr 20 '20 at 09:08
  • 1
    The code in the question correctly checks the value returned from `std::getline`. `std::getline` returns a reference to the stream it was reading; streams have a conversion to `bool` that indicates whether their state is good. Returning a reference from `std::getline` is a convenience, so that you don't have to check separately. But you can always check the stream that you're using to see whether it's in a good state, in exactly the same way that you checked it in the question. – Pete Becker Apr 20 '20 at 13:01

0 Answers0