I was going through a code in my school textbook, wherein there is a line who's function is to clear the input buffer (mentioned as a comment in the code).
I couldn't quite understand its purpose. It is definitely required as it's removal messes up the console input process.
Please explain what's its function, and what is happening when I remove it.
I have also tried using cin.ignore(); and it works just fine too. How is the function used here, its replacement?
P.S. In school we are using the older version of C++. Hence the ".h" extension, clrscr();, etc.
When the code reach cin.get(name,30);, it'll read everything from the input buffer until a newline is reached.
After writing your marks, you press the enter key. This action is stored in input buffer as a newline... So, if you don't clear the buffer, the code will read only a newline every time is to write the name of the student --> You don't get any chance of writing anything.
Removing cin.get(ch);, the output will be something like:
So this means I can remove the fin.get(ch); at line no. 29, because 'marks' is not using a get() function, i.e. it isn't using the buffer to get its value?