cwindowsinputcmd

clear cmd input in C


For example, I run the program:

int a = 0;
while(a < 1000000000){
    a++;
}

and at this moment I'll type into the terminal:

a\na\na\na\na\na\n

Obviously, the text I entered will not be read, so when the program stops, I get something like this:

'a' is not recognized as an internal or external command,
operable program or batch file.

'a' is not recognized as an internal or external command,
operable program or batch file.

How do I clear this input and is it possible at all?

I tried writing after this loop fseek(stdin,0,SEEK_END); or while(getchar() != EOF); but in the second case it waits for EOF and I didn't provide it, and it also shows that I entered something:

C:....>test
a
a
a
a
a

help me with this question and I would like to add that besides the standard libraries I can use <windows.h>


Solution

  • You could call FlushConsoleInputBuffer just before you exit the program:

    #include <Windows.h>
    int main(void) {
        int a = 0;
        while(a < 1000000000){
            a++;
        }
        FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)); // last thing you do
    }