fgets不将提供的输入存储在目标变量中 [英] fgets not storing the supplied inputs in the destination variables

查看:74
本文介绍了fgets不将提供的输入存储在目标变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时:

#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;
}

我得到以下输出:

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)) ===|

它完全忽略 fgets ,并且不提示输入任何内容.始终将if语句视为true.并且始终将(null)用作 name .

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

您能告诉我我的代码有什么问题吗?曾经有人告诉我使用 fgets 而不是 scanf gets .值得一提的是 scanf 也给我带来了类似的问题.

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.

推荐答案

在您的代码中, name age gender male 都是 char 变量,而不是 char 数组.您将需要一个数组来实现您的目标.数组的大小必须与传递给 fgets()的大小相同.

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().

说,

  • atoi() 不会转换提供的字符串本身为 int ,它将返回转换后的值.您必须将其存储在变量中.
  • male 是一个变量,而不是字符串文字,因此变量名称不能用作比较.您可以定义一个包含字符串文字的变量 const char * match ="male"; 或直接使用字符串文字本身("male" )进行比较./li>
  • 您需要使用 strcmp() 进行比较字符串.
  • 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.

这篇关于fgets不将提供的输入存储在目标变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆