fgets not storing the supplied inputs in the desti

2019-09-27 16:41发布

问题:

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)

And these are the warnings i get from the compiler in my codeblocks:

||=== Build: Release in justexploring1 (compiler: GNU GCC Compiler) ===|
D:\Project\C language\justexploring1\main.c||In function `main':|
D:\Project\C language\justexploring1\main.c|8|warning: passing arg 1 of `fgets' makes pointer from integer without a cast|
D:\Project\C language\justexploring1\main.c|10|warning: passing arg 1 of `fgets' makes pointer from integer without a cast|
D:\Project\C language\justexploring1\main.c|12|warning: passing arg 1 of `fgets' makes pointer from integer without a cast|
D:\Project\C language\justexploring1\main.c|13|warning: passing arg 1 of `atoi' makes pointer from integer without a cast|
D:\Project\C language\justexploring1\main.c|16|warning: format argument is not a pointer (arg 2)|
D:\Project\C language\justexploring1\main.c|17|warning: format argument is not a pointer (arg 2)|
D:\Project\C language\justexploring1\main.c|6|warning: 'name' might be used uninitialized in this function|
D:\Project\C language\justexploring1\main.c|6|warning: 'age' might be used uninitialized in this function|
D:\Project\C language\justexploring1\main.c|6|warning: 'gender' might be used uninitialized in this function|
D:\Project\C language\justexploring1\main.c|6|warning: 'male' might be used uninitialized in this function|
||=== Build finished: 0 error(s), 10 warning(s) (0 minute(s), 0 second(s)) ===|

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

Could 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.

回答1:

In your code, name, age, gender, male all are char variables, not char array. You'll be needing an array to achieve what you're targeting. Your arrays need to be of the same size as of that what you pass to fgets().

That said,

  • atoi() does not convert the supplied string itself to int, it returns the converted value. You have to store that in a variable.
  • male is a variable, not a string literal, so the variable name cannot be used as a value for comparison. You can either define a variable holding the string literal, const char * match = "male"; or directly use the string literal itself ("male") for comparison.
  • You need to use strcmp() for comparing strings, anyway.


标签: c fgets