Flushing the input buffer.
Well, when my program runs the block of code below, I enter help for the input, and it does the correct thing, but when system("PAUSE"); comes up and I hit a key, the console window flashes, and wont do anything. I ultimately have to shut it down with the X at the top right-hand corner, which locks up my CPU. DA has told me that I need to flush the input buffer, so I used fflush(stdin); which was the only thing I know. What am I doing wrong?
Code:
/* this is the code that I think is the culprit*/
clearscreen
_header
br
int cmndline;
cout<<"INPUT: ";
cin>>cmndline;
if(strcmp("string help", "string help") ==0)
{
fflush(stdin);
cout<<"hello";
br
pause
goto startofprog;
}
br, _header, clearscreen, and pause are all macros.
Pause is a macro for system("PAUSE");
if you want to compile this for yourslf, use the following block of code:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
int main()
{
startofprog:int cmndline;
cout<<"INPUT: ";
cin>>cmndline;
if(strcmp("string help", "string help") ==0)
{
fflush(stdin);
cout<<"hello";
cout<<endl;
system("PAUSE");
goto startofprog;
}
return 0;
}
Thanks in advance for any help!