I know there are a lot of questions about this, but i need a solution to flush the STDIN input buffer without pressing any key or 'enter'.
I'm using fgets(char, size, char)
(on a loop) to get an input stream from the keyboard and I want to press just 1 'enter' and go to the next input WITHOUT allocating the stdin buffer (if the buffer is bigger than the one specified on fgets, the first char array will be good but the next fgets on the loop will write the rest of the buffer on the next char array)
Most of the solutions on SO are like this:
/** deletes the buffer on stdin */
void flushStdin(){
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
But this needs to press 'enter'. My whole code looks like this:
void readString(char *arrayString){
fgets(arrayString, BUF_SIZE * sizeof(arrayString), stdin); //1st Enter here
flushStdin(); //2nd Enter here
//Deletes the newline '\n' at the end
size_t len = strlen(arrayString);
if (len > 0 && arrayString[len-1] == '\n')
arrayString[--len] = '\0';
return;
}
Thank you!