我正在尝试制作BMI计算器。我得到了多个"未声明的标识符"即使我已经犯了错误,也会出错?
#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
答案 0 :(得分:1)
我测试了你的代码,它工作正常!代码中存在错误,例如:
printf("Your BMI is: %d", &bmi);
你只需要像这样打印:
printf("Your BMI is: %d", bmi);
答案 1 :(得分:0)
你的编译器已经老了,它希望你做一些并非真正错误的事情,就像几年前它的编程方式一样。
您的代码也有问题:
printf("Your BMI is: %d", &bmi);
将其更改为:
printf("Your BMI is: %d", bmi);
答案 2 :(得分:0)
根据您提供的信息,最可能的候选者是您的编译器正在执行C89规则,该规则要求所有变量声明都放在块的开头。请看以下示例:
#include <stdio.h>
int main (void)
{
printf("Welcome to my program\n");
int x = 5;
printf("x = %d\n", x);
return 0;
}
我可以使gcc
更加挑剔:
$ gcc -pedantic-errors -std=c89 -c vars.c
vars.c: In function ‘main’:
vars.c:7:3: error: ISO C90 forbids mixed declarations and code [-pedantic]
要修复此错误,必须将变量声明提升到块的顶部:
#include <stdio.h>
int main (void)
{
int x = 5;
printf("Welcome to my program\n");
printf("x = %d\n", x);
return 0;
}
现在它将以这种方式构建得很好。默认情况下您看到此行为的事实可能意味着您正在使用旧的编译器(或者可能是仅支持c89的专用编译器)。