荆襄楚樊 发表于 2020-10-18 11:02:46

帮忙看一下错在什么地方?

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

int main(void)
{
    float weight;
    float volume;
    int size;
    int letters;
    char name;         // 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:03:30

出现Hi! What's your first name?
sharla
再按一次回车进行下一个输入

jackz007 发表于 2020-10-18 12:13:47

本帖最后由 jackz007 于 2020-10-18 12:25 编辑

      scanf_s() 在输入字符串时,必须有一个参数,用来指明用于保存字符串变量的字符容量。
      为这一句添加蓝色字符部分试试看呢。
      scanf_s("%s", name,40) ;

荆襄楚樊 发表于 2020-10-18 14:17:10

巴巴鲁 发表于 2020-10-18 12:03
出现Hi! What's your first name?
sharla
再按一次回车进行下一个输入

我试了不行。谢谢你
页: [1]
查看完整版本: 帮忙看一下错在什么地方?