vendredi 8 mai 2015

How not to receive a char which is not a number in scanf for double values.

I want in this little piece of code to insert double values and get the returned values as written, but in case I enter another character, besides numbers, I want the returned value to be 0.

here is my code:

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

int LegalInputChecker(double Move);

int main()
{
    double Move;
    int res;
    scanf("%lf", &Move);

   res = LegalInputChecker(Move);

   return res;

}

int LegalInputChecker(double Move)
{

    if(Move-(int)Move == 0 && (Move>0 && Move<=7))
    return 1; 

    else if((Move<0 || Move>7) && (Move-(int)Move == 0))
    return 2; 

    else if(Move==0)
    return 3;  

    else
    return 0;

}

for some reason, when I enter a letter for example, I keep getting the returned value "3", whats the reason?

Aucun commentaire:

Enregistrer un commentaire