BMI计算器,C语言,未声明标识符

BMI计算器,C语言,未声明标识符,c,C,我想做一个体重指数计算器。我得到了多个“未声明的标识符”错误,即使我已经犯了它们 #include <stdio.h> int main(void) {//main method //Ex 2.32 printf("Ex 2.32: Body Mass Index Calculator\n\n"); int weightInPounds; int heightInInches; int bmi; //displays title

我想做一个体重指数计算器。我得到了多个“未声明的标识符”错误,即使我已经犯了它们

#include <stdio.h>

int main(void)
{//main method

    //Ex 2.32
    printf("Ex 2.32: Body Mass Index Calculator\n\n");

    int weightInPounds;
    int heightInInches;
    int bmi;

    //displays title
    printf("Body Mass Index Calculator\n");

    //user input for weight
    printf("Please input your weight in pounds:\n");
    scanf("%d", &weightInPounds);

    //user input for height
    printf("Please input your height in inches:\n");
    scanf("%d", &heightInInches);

    //caluclate BMI
    bmi = (weightInPounds * 703) / (heightInInches*heightInInches);
    printf("\n");   

    //display BMI categories
    printf("BMI Values\n");
    printf("Underweight: less than 18.5\n");
    printf("Normal: between 18.5 and 24.9\n");
    printf("Overweight: between 25 and 29.9\n");
    printf("Obese: 30 or greater\n\n");

    //display user BMI
    printf("Your BMI is: %d", &bmi);
    //end Ex 2.32

}//end main function
#包括
内部主(空)
{//main方法
//ex2.32
printf(“Ex 2.32:体重指数计算器\n\n”);
整数加权整数;
内部高度英寸;
体重指数;
//显示标题
printf(“体重指数计算器”);
//用户输入重量
printf(“请以磅为单位输入您的体重:\n”);
scanf(“%d”、&weightInPounds);
//高度的用户输入
printf(“请以英寸为单位输入您的身高:\n”);
扫描频率(“%d”和高度英寸);
//钙盐体重指数
体重指数=(重量磅*703)/(高度英寸*高度英寸);
printf(“\n”);
//显示体重指数类别
printf(“BMI值”);
printf(“体重不足:小于18.5\n”);
printf(“正常:介于18.5和24.9之间\n”);
printf(“超重:介于25和29.9之间”);
printf(“肥胖:30或以上\n\n”);
//显示用户体重指数
printf(“您的体重指数为:%d”,&BMI);
//完二点三二
}//终端主功能

我测试了你的代码,效果很好!代码中存在以下错误:

printf("Your BMI is: %d", &bmi); 
您只需按如下方式打印:

printf("Your BMI is: %d", bmi);

您的编译器很旧,它希望您做一些并非真正错误的事情,就像几年前它是如何编程的

您的代码也有问题:
printf(“您的体重指数为:%d”,&BMI)

将其更改为:

printf("Your BMI is: %d", bmi);

根据您提供的信息,最有可能的情况是编译器正在强制执行C89规则,该规则要求所有变量声明都放在块的开头。以以下为例:

#include <stdio.h>

int main (void)
{
  printf("Welcome to my program\n");

  int x = 5;
  printf("x = %d\n", x);

  return 0;
}
要修复此错误,必须将变量声明提升到块的顶部:

#include <stdio.h>

int main (void)
{
  int x = 5;

  printf("Welcome to my program\n");
  printf("x = %d\n", x);

  return 0;
}
#包括
内部主(空)
{
int x=5;
printf(“欢迎使用我的程序”\n);
printf(“x=%d\n”,x);
返回0;
}

现在,它将以这种方式建造。默认情况下,您会看到这种行为,这可能意味着您使用的是旧编译器(或者可能是只支持c89的专用编译器)。

在c89中,所有变量声明都必须出现在块的开头。也许您的编译器正在强制执行该操作?你能粘贴完整的错误吗?OT:
printf(“你的BMI是:%d”,&BMI)必须是
printf(“您的BMI为:%d”,BMI)
@FatalError,您应该将注释升级为答案。当您将第一个
printf
语句移动到变量声明之后的点时会发生什么?如果你使用的是一个旧的编译器,那会有所不同。你应该把它作为一个评论发布,它不是一个被接受和否决的回复。但也许你应该在答案中解释一下。只需向上看28个代表即可:-)@Nicolas在读取整数时,您是否建议使用不带地址运算符的scanf?!
#include <stdio.h>

int main (void)
{
  int x = 5;

  printf("Welcome to my program\n");
  printf("x = %d\n", x);

  return 0;
}