I've read a few articles and forum threads about the correct way to use cin.ignore and my conclusion is that it would be a good practice to use cin.ignore (with or without parameters) after every cin >> variable, in case the next keyboard input is read using getline(cin,variable), to avoid getting an empty variable because of the '\n' left in the buffer, and the user input to be stuck in the buffer.
Is that correct?
without parameter you do not remove the new line character '\n'.
You can use ignore after each '>>', but it is not necessary. Use it before getline() or if you don't know what comes next, i.e. at the end of a function.
Thanks!
But I'm still a bit confused because of this problem I had a few days ago: www.cplusplus.com/forum/beginner/176292/
In this case, using cin.ignore right before getline caused the input to be ignored.
So I thought the best way to use it would be after each cin >> variable so I would leave the buffer "ready" in case the next input is read using getline. I think of those cases where cin is not immediately followed by a getline, but maybe the program execution leads to different function calls and one of them includes a getline.
One example where extra ignores could break user input: I often enter every parameter at once. So if program asks me to enter x then it asks for y and finally for z, I would enter something like 2 3 4 and will expect it to read that. So it would ignore my input.
On the other hand you can document that your program expects each value on its own line and this becomes fine. Annoying, but fine.
There is no magical "do this and you will never have problems" (however there is "this is default choice unless it is not suited for your needs") or "never ever do this" (however there is "This is almost always wrong, make sure you really need this").
You should understand how streams work and how console behaves (when it waits for input, when it does, when data is actually sent to your program)
> without parameter you do not remove the new line character '\n'. istream& ignore (streamsize n = 1, int delim = EOF);
you'll extract one character, because you just ended user input it's expected for that character to be a whitespace, hopefully a line break.
Another reason for not using std::ws is if empty lines are valid input.
> I would enter something like 2 3 4 and will expect it to read that. So it would ignore my input.
If you just discar one character that kind of format would still work.