I have been using the code
while (getchar() != '\n');
to clear everything in stdin
in my programs. However, I am running into a slight issue on some occasions. There are times in my programs where when I call getchar()
, there may or may not be anything in stdin
. When it is the case that there is nothing in stdin
, what ends up happening is getchar()
is called and it is waiting for something in stdin
. I then have to enter \n on my keyboard otherwise the program is just stuck on that one line. I am wondering if there is a simple way to first check if stdin
is empty before doing while (getchar() != '\n');
in order to avoid this problem?
Not sure if the compiler matters for the answer, but I am using Visual Studio 2017. Edit: when I say stdin, I am referring to characters entered by the keyboard