私は現在Cプログラミングを練習しており、BMI計算機をいくつか作っています。それは素晴らしいが、最初のscanf
のユーザー入力文字列または文字がデータをfloat変数 'weight'に送信するときに機能します。それ以来、それはすべてscanf
を通過し、私にはエラーYour BMI is 1.#J
を示した。このエラーを解決するには?ここに私のコードです。あなたは、単一のfloat値に読んでいるよう浮動小数点変数を保持する文字列をscanfに入力したときにエラーが発生する
#include <stdio.h>
int main()
{
float weight;
float height;
float result;
float c_height;
printf("Enter your weight as kilogram...\n\n> ");
scanf("%f",&weight);
//If user input char or string , it passed all thing and showed error.
printf("\nEnter your height as centimetres...\n\n> ");
scanf("%f",&height);
c_height=(height/100)*(height/100);
result=weight/c_height;
if(result<18.50)
{
printf("\nYour BMI is %.2f\n\n",result);
printf("You are underweight! Try to eat more frequently\n\n");
printf("Thank you for using my program!\n\n");
}else if(result>=18.50 && result<22.90)
{
printf("\nYour BMI is %.2f\n\n",result);
printf("You are healthy! Keep eating healthy\n\n");
printf("Thank you for using my program!\n\n");
}else if(result>=22.90 && result<24.90)
{
printf("\nYour BMI is %.2f\n\n",result);
printf("You are a little overweight! Avoid eating some fat and an oil\n\n");
printf("Thank you for using my program!\n\n");
}else if(result>=24.90 && result<29.90)
{
printf("\nYour BMI is %.2f\n\n",result);
printf("You are overweight! Avoid eating fat and do exercise often\n\n");
printf("Thank you for using my program!\n\n");
}else if(result>=29.90)
{
printf("\nYour BMI is %.2f\n\n",result);
printf("You are obese! Do exercise everyday and eat carefully!\n\n");
printf("Thank you for using my program!\n\n");
}else
{
printf("Error occured!!");
}
return 0;
}
scanfの後誰かが重量を求めているときに文字列や浮動小数点数[フォーマット指定子](https://www.le.ac.uk/users/rjm1/cotter/page_30.htm) – wrangler