Well I been doing a lot of searching on google and on here about how to flush the input stream properly. All I hear is mixed arguments about how fflush() is undefined for the input stream, and some say just do it that way, and others just say don't do it, I haven't had much luck on finding a clear efficient/proper way of doing so, that the majority of people agree on.. I am quite new at programming so I don't know all the syntax/tricks of the language yet, so my question which way is the most efficient/proper solution to clearing the C input stream??
Use the
getchar()
twice before I try to receive more input?Just use the
fflush()
function on the input? orThis is how I thought I should do it.
void clearInputBuf(void); void clearInputBuf(void) { int garbageCollector; while ((garbageCollector = getchar()) != '\n' && garbageCollector != EOF) {} }
So, whenever I need to read a new scanf(), or use getchar() to pause the program I just call the clearInputBuf.. So what would be the best way out of the three solutions or is there a even better option?