我的程序写入文本文件后,我得到了奇怪的字符。例如:
Enter Weight in Pounds and Height in Inches: ÍÍÍÍYour BMI is: 25.11
超重
这些“ÍÍÍÍ”应为数字,即:170 70.
还有其他人遇到过这个问题吗?如果你需要在这里查看我的代码。
#include <stdio.h>
#include <stdlib.h>
FILE *fp;
void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double BMI);
int main()
{
fp = fopen("csis.txt", "w");
int i;
double Wieght_in_Pounds, Hieght_in_Inches, BMI;
Wieght_in_Pounds = 0;
Hieght_in_Inches = 0;
BMI = 0;
for (i = 1; i <= 4; ++i) {
Calculate_BMI(Wieght_in_Pounds, Hieght_in_Inches, BMI);
}
fclose(fp);
system("pause");
return(0);
}
void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double
BMI)
{
printf("Enter Weight in Pounds and Hieght in Inches: ");
fprintf(fp, "Enter Weight in Pounds and Hieght in Inches: ");
scanf(" %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
fscanf(fp, " %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
BMI = (Weight_in_Pounds * 703) / (Hieght_in_Inches * Hieght_in_Inches);
printf("Your BMI is: %3.2lf\n", BMI);
fprintf(fp, "Your BMI is: %3.2lf\n", BMI);
if (BMI < 18.5) {
printf("Under Weight\n");
fprintf(fp, "Under Weight\n");
}
else if (BMI >= 18.5, BMI <= 25.0){
printf("Normal weight\n");
fprintf(fp, "Normal weight\n");
}
else if (BMI >= 25.0, BMI <= 30.0) {
printf("Overweight\n");
fprintf(fp, "Overweight\n");
}
else {
printf("Obese\n");
fprintf(fp, "Obese\n");
}
}
答案 0 :(得分:1)
以写入模式(fscanf()
)打开的文件上的操作w
无效。删除fscanf()
声明。
在,
语句中使用&&
运算符替换if
。 comma
(,
)运算符的结果与序列中最右边的操作数的类型和值相同。因此,只考虑BMI <= 25.0
和BMI <= 30.0
的结果。
if (BMI >= 25.0 && BMI <= 30.0)
另外,请不要忘记查看fopen()
和scanf()
的返回值。