Scanf 用于两个字符串和一个浮点数 [英] Scanf for a two strings and a float number

查看:53
本文介绍了Scanf 用于两个字符串和一个浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个菜单,需要接受用户的三个输入.

I am creating a menu that needs to take in an three inputs from the users.

    char *fullname;
    char *date;
    float sal;
    printf("\nEnter full name: ");

第 92 行

scanf("%s", &fullname);
printf("\nEnter hire date: ");

第 94 行

scanf("%s", &date);
printf("\nEnter salary: ");

第 96 行

scanf("%d", &sal);

这些是我收到的错误

Employee.c:92: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:94: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
Employee.c:96: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘float *’

我能否解释一下导致这些问题的原因?

Can I get an explanation of what is causing these issues?

推荐答案

有几个问题:

首先:

当您对字符串使用 scanf 时,您不使用 &.所以只需 scanf("%s", fullname);.

When you use scanf for strings you do not use the &. So just scanf("%s", fullname);.

第二:

您的指针未初始化.试试这个:

Your pointers aren't initialized. Try this instead:

char fullname[256];
char date[256];

只要您最多输入 255 个字符,这将起作用.

This will work as long as you input at most 255 characters.

第三:

您输入的最后一个 scanf 不匹配.当您在格式字符串中指定了 int 时,您将传入一个 float.试试这个:

Your typing for the last scanf doesn't match. You're passing in a float when you've specified an int in the format string. Try this:

scanf("%f", &sal);

这篇关于Scanf 用于两个字符串和一个浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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