1
votes

Here is an example code demonstrating the problem I'm facing.

#include <iostream>
#include <string>
extern "C" {
#include <unistd.h>
}

int main()
{
    std::cout << "Making tests ready!" << std::endl;
    std::cout << "\nTo start out, Enter an integer: ";
    int a = 0;
    std::cin >> a;
    std::string input;
    sleep(3);       // what to do if user enters data during this?
    std::cout << "\n Now enter a string";
    std::getline(std::cin, input);
    std::cout << "\nHere are your values - " << a << " & " << input;
    return 0;
}

See the sleep call in between the code? This could be replaced with somewhat long delays while computing something when my program isn't accepting any inputs. Now if user presses some keys during this time, that input is captured by std::getline() in next line of code. I know this is the default behavior since it should capture the input being provided.

But what I want is to clear all that captured input and start fresh with 15th line that is std::cout << "\n Now enter a string";, which is immediately after sleep. I don't know exact term to describe this or else I would have used that. Thanking you.

Edit: I've tried using std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in my code, but it asks for input and then discards it.


Please mind my english, not a native speaker.

1
Just ignore all input until there is nothing more to read. You can peek to see if there's more input. - Some programmer dude
@JoachimPileborg , I tried using std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in my code but that was stopping for input :/ - gw0
Like cin or getline() stops for some input. and when I tried entering something, it again asked for input (this time for actual getline statement) and then printed only 2nd time one. Meaning that it discarded my first input. - gw0
Do you want to discard everything that was typed during a specific time interval (e.g. while the worker function is running)? Note that this only makes sense for terminals and terminal-like input streams, not for arbitrary files. C++ streams library has no functionality specific for terminals. - n. 1.8e9-where's-my-share m.
Ohhh I actually understood it, its asking for input in those cases where there wasn't any extra input. But I'm not sure how to check whether there is some extra input or not - gw0

1 Answers

0
votes

Reading the comments causes me to think that you can't really solve this problem (at least by the means suggested there). There's an inherent race condition in any case. Consider the following lines:

sleep(3);       
// (*) <- Potential location 1.
std::cout << "\n Now enter a string";
// (**) <- Potential location 2.
std::getline(std::cin, input);

The various comments show some (very technically-competent) ways to flush the standard input. The problem is, you cannot put them in the location marked (*) nor (**).

  • First location - you clear the standard input some way. Now you decide it's time to move to the next line (std::cout << ...). While you do that, the user types in some more input. Race!

  • Second location - You print out the message (std::cout << ...), and proceed to clear the standard input. Before you manage to do that, the user typed in something. Race!


It seems to me that any of the techniques described in the comment require locking the standard input, and I don't think there's a (standard) way to do so.