#Ensure Input Buffer isn't empty before cin.ignore();
17 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
this seems like an x-y-problem
The "XY problem" occurs when someone asks about an attempted solution rather than the problem itself. This makes it difficult to provide appropriate help and guide users to better solutions.
It's just a question if the Solution I came up with is safe. It works, but I don't know if it is insecure or smth...
So is there a better Optiont?
how are you using this?
by default, terminals are line-buffered
so by default, the buffer in std::cin will always either be empty or end with a newline
but you said you wanted to ignore everything in the input buffer, not just everything on the current line?
I want to ignore everything in the Input buffer, to ensure, when calling std::getline(std::cin, input), my loop doesn't go through it twice. At the same time, when ignoring everything in the Input buffer and nothing is there, std::cin.ignore waits until something is in it. That's why I am pushing something to it to ensure it is not empty...
you can use in_avail() to see if there are any characters available
but usually you should already know whether there's a newline character: when you're using >>, you don't consume a trailing newline, but when you're using getline you do
It is for an Library, so I don't know if any input function was already called...
if it's for a library, you might not want to hardcode std::cin
generally, you want to separate the part where you actually get the input as much as possible from the part where you deal with the input
so instead of having a function that reads from std::cin and then computes something with the input, you'd have the function take the input as a C++ type and make it the caller's responsibility to get the actual input