This is a BMI Calculator..That's no problem when using correctly.(Typing correct number)
But, if I'm a bad guy and typing some letters…It would show “Don't trick me, Again(in Meters):” repeatedly but wouldn't let me typing anything(Like just skip "scanf")
Code:
#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); //be skipped?
}
if(!height || !weight) OK = 0;
}
BMI = weight / (height * height);
printf("\nOk..your BMI is: %.2f\n", BMI);
system("pause");
return 0;
}