ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result] - scanf ("% d", & x);

1

please could you tell me why the error persists if the scanf is validated. The error I get when I use the library 'mpi'. This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int insertminimo ();

int main(int argc, char *argv[])  
{
    int myid, numproc;
    int mystart, myend;
    int n = 100;

    /* Inicializa */
    MPI_Init(&argc, &argv);

    /* conseguir rank y # de procesadores size */
    MPI_Comm_size(MPI_COMM_WORLD,&numproc);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);

    int minimo;
    minimo = insertminimo ();

    /*Aqui va el resto de MPI ....*/
    return 0;
}
int insertminimo ()
{
        int x;  
        printf ("Cuál es la longitud mínima?\n=> ");  
        //scanf ("%d", &x);
        if (scanf("%d", &x) != 1)
        {
           fprintf( stderr, "Previsto el ingreso de longitud mínima como entrada\n");
           exit(1);
        }

        if(x <=-1)  
        {  
            printf("\n\n**El numero insertado excede la capacidad,"  
                    "por favor ingrese la longitud mínima mayor a cero**\n\n");  
            printf ("Cuál es la longitud mínima?\n");  
            scanf ("%d", &x);  
        }

        return x;
}
    
asked by user5672720 08.05.2016 в 18:29
source

1 answer

1

The same error message indicates, the return value of scanf() should not be ignored , this is by the compiler, you could try validating:

...
...
if(x <=-1)  
    {  
     printf("\n\n**El numero insertado excede la capacidad, por favor ingrese la longitud mínima mayor a cero**\n\n");  
     printf ("Cuál es la longitud mínima?\n");  
    if (scanf("%d", &x) <= 0) {
        printf("%d", x);
    } else {
        printf("valor de longitud mínima es incorrecto.\n");
    }
}

return x;
...
...
    
answered by 09.05.2016 / 02:59
source