鱼C论坛

 找回密码
 立即注册
查看: 589|回复: 3

[已解决]帮忙看一下错在什么地方?

[复制链接]
发表于 2020-10-18 11:02:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <string.h>      //提供strlen()函数的原型
#define DENSITY 62.4     //人的密度(单位是:英镑/每立方英尺)

int main(void)
{
    float weight;
    float volume;
    int size;
    int letters;
    char name[40];         // name 有一个有40个字符的数组

    printf("Hi! What's your first name?\n");
    scanf_s("%s", name);
    printf("%s, what's your weight in pounds?\n", name);
    scanf_s("%f", &weight);

    size = sizeof name;
    letters = strlen(name);
    volume = weight / DENSITY;

    printf("Well, %s, your volume is %2.2f cubic feet.\n",
        name, volume);
    printf("Also, your first name has %d letters,\n",
        letters);
    printf("and we have %d bytes to store it in.\n", size);

    return 0;
}

/*
Win10 VS2019
---------------------
本人答案:
Hi! What's your first name?
sharla
---------------------
书本答案:
Hi! What's your first name?
sharla
sharla, what's your weight in pounds?
139
Well, Sharla, your volume is 2.23 cubic feet.
Aiso, your first name has 6 letters,
and We have 40 bytes to store it in.
*/
最佳答案
2020-10-18 12:13:47
本帖最后由 jackz007 于 2020-10-18 12:25 编辑

        scanf_s() 在输入字符串时,必须有一个参数,用来指明用于保存字符串变量的字符容量。
        为这一句添加蓝色字符部分试试看呢。
        scanf_s("%s", name,40) ;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-18 12:03:30 | 显示全部楼层
出现Hi! What's your first name?
sharla
再按一次回车进行下一个输入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-18 12:13:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-10-18 12:25 编辑

        scanf_s() 在输入字符串时,必须有一个参数,用来指明用于保存字符串变量的字符容量。
        为这一句添加蓝色字符部分试试看呢。
        scanf_s("%s", name,40) ;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-18 14:17:10 | 显示全部楼层
巴巴鲁 发表于 2020-10-18 12:03
出现Hi! What's your first name?
sharla
再按一次回车进行下一个输入

我试了不行。谢谢你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 01:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表