Alternate for fflush(stdin) in c , for c++ . Clearing Input Buffer in c++

The problem comes when a string is inputted after a number. Because the "Enter" pressed after the number still remains in the standard input buffer. In C language the problem can be solved using:
 fflush(stdin);
use it after line used to input number. 
 The above code will clear the enter from input buffer.


Similar situation comes in c++;
Well there is a way to solve this problem.

1.Create a variable of char type.
2.Call the getc() method using object of standard input class istream.


char ch;
cin.getc(ch);


However the code only removes a single character from the input buffer.!



No comments:

Post a Comment