I can not clean the keyboard buffer in C

3

In my code, I request a series of data from the user using the gets() function. The problem is that at a certain point in the program when requesting a data with gets() it simply jumps to the next request and leaves the variable blank , I tried to clean the buffer in the following way and it does not work:

fflush(stdin);

This is the code:

#include <stdio.h>
#include <string.h>

struct alumnos{
    char nombre[10];
    char dni[20];
    int edad;
};

int main(){
    struct alumnos myalumno[3];
    int i;
    for(i=0;i<=2;i++){
        printf("Ingrese el nombre del alumno %i: ",i+1);
        fflush(stdin);
        gets(myalumno[i].nombre);
        printf("Ingrese el DNI del alumno %i: ",i+1);
        fflush(stdin);
        gets(myalumno[i].dni);
        printf("Ingrese la edad del alumno %i: ",i+1);
        fflush(stdin);
        scanf("%i",&myalumno[i].edad);
    }
}
    
asked by Cristofer Fuentes 14.07.2016 в 03:01
source

2 answers

4

Taken from here that fflush is only used in output buffers.

Try using fpurge :

int main() {
    struct alumnos myalumno[3];
    int i;
    for(i = 0; i <= 2; i++) {
        printf("Ingrese el nombre del alumno %i: ",i+1);
        fpurge(stdin);
        gets(myalumno[i].nombre);
        printf("Ingrese el DNI del alumno %i: ",i+1);
        fpurge(stdin);
        gets(myalumno[i].dni);
        printf("Ingrese la edad del alumno %i: ",i+1);
        fpurge(stdin);
        scanf("%i",&myalumno[i].edad);
    }
    return 0;
}

Where ...

If fflush is given NULL per parameter, it will clean all output buffers that are active, but fpurge only takes the input buffer and cleans it.

You must be careful with the use of fpurge , it is not a "portable" solution to other platforms, so you should be careful when using it.

Reference: fpurge

With that it should work.

    
answered by 14.07.2016 / 03:16
source
1

Since the behavior of fflush is not defined by the standard for the input flows, it is only sure that it works for the outgoing flows (sending the remaining content of the buffer to the output). Therefore fflush should not use it.

It is also not advisable to use fpurge since it is not defined in standard C nor is it portable to all platforms (as they have explained in other comments). In fact, in Linux it is not available, although there is a void __fpurge(FILE *stream) function that does the same and is inside the standard GNU library (glibc).

The possible solution is to manually write the code that cleans the input buffer until you find a line break. The two options you have are:

char c;
while ((c = getchar()) != '\n' && c != EOF);

and the one that seems best to me:

scanf("%*[^\n]%*c");

In the last option, you are telling scanf first to ignore (use of *) any number of non-characters blank (other than tab '\t' , space ' ' or line break '\n' ) with the %*[^\n] specifier until you find a blank character (the line break), in which case ignore it with %*c .

The %*c can not be replaced by \n because in that case you would be ordering at that point to ignore any number of blank characters until you find one that is not, causing the scanf function to remain pending that you enter any non-blank character, which will not be read and will remain pending in the input buffer for the next call to scanf .

Keep in mind that these solutions are valid in the case that the buffer has content other than blank characters ( ' ', '\t' o '\n' ). If the buffer is empty, it will wait for keyboard data other than blank characters to be entered (data that will be ignored).

    
answered by 25.06.2017 в 18:14