So what do these mean in detail? (I like to know the context behind things)

std::cin.clear();

std::cin.ignore();

We have only been told to implement them in the code for now, as we won’t be using them in Unreal. I am just curious to the details as to how they work, and/or if they are used more commonly elsewhere/for other errors?

std::cin.clear() clears the fail bit which would be set if something went wrong e.g. you tried to extract an int into a char

char test;
std::cin >> test;

If 2 was entered here then that would set the fail bit. If that is set then std::cin will no longer attempt to extract characters e.g.

int again;
std::cin >> again;

Would do nothing. Calling std::cin.clear() puts std::cin back into a “good” state. However std::cin is still holding that 2 which be used instead of getting new input.
So if instead of again you were trying to do the same thing (using the first example with test) then that would result in the exact same situation.

std::cin.ignore() extracts a character and discards it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms