lundi 22 août 2016

C ignores (fgets) function in my program

when I run this code:

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

   int main()
{
    char name , age , gender , male;

    printf("Please enter your name \n");

    fgets(name, 20 ,stdin);

    printf("Please enter your age \n");

    fgets(age , 2 , stdin);

    printf("Please enter your gender \n");

    fgets(gender , 7 , stdin);

    atoi(age);

{
    if (age < 50 && gender == male)

        printf(" You're fit to play\n Welcome player ,%s \n",name);

        else printf("Sorry , %s. You're not fit to play", name);

}
return 0;
}

I get this output

please enter your name please enter your age please enter your gender you're fit to play welcome player, (null)

It totally ignores fgets and doesn't prompt for any input. always treats if statement as true. and always uses (null) for name.

you please tell me what's wrong with my code? I was once told to use fgets instead of scanf and gets it's worth mentioning that scanf also gave me similar problems.

Aucun commentaire:

Enregistrer un commentaire