scanf 无法使用?
这是自己做的 BMI 计算器…如果正常输入数字的话就没有问题但如果我打英文的话…会不断出现 “Don't trick me, Again(in Meters):” 但是不会让我输入字串…
程序代码:
#include<stdio.h> #include<stdlib.h> int main(void) { float height = 0, weight = 0, BMI = 0; int OK = 0; printf("Welcome to BMI calculator.\nPlease type your height first(in Meters):"); while(!OK){ OK = scanf("%f", &height); if(height >0 && OK){ printf("\nWell, not so tall :)\nAnd..Your Weight(in Kilograms):"); OK = scanf("%f", &weight); while(weight <= 0 || !OK) { printf("\nAgain(in Kilograms):"); OK = scanf("%f", &weight); } } else { printf("\nDon't trick me, Again(in Meters):"); OK = scanf("%f", &height); } if(!height || !weight) OK = 0; } BMI = weight / (height * height); printf("\nOk..your BMI is: %.2f\n", BMI); system("pause"); return 0; }