#include <stdio.h>
float pounds = 0, inches = 0, BMI = 0;
float kg = 0, meters = 0;
char input[100], choice;
int main()
{
start:
do {
printf("\n\nThis program is used to calculate the BMI of a person.\n\n\n");
printf("What kind of BMI do you want to calculate?\n");
printf("(Imperial(i/I) or Metric(m/M) ):\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%c", &choice);
if ((choice == 'i') || (choice == 'I')) {
printf("Enter you weight in pounds:\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", £s);
printf("Enter your height in inches:\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &inches);
BMI = (pounds * 703) / (inches * inches); // BMI Imperial formula.
printf("Your BMI is %.2f\n\n", BMI);
} else if ((choice == 'm') || (choice == 'M')) {
printf("Enter your weight in kg:\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &kg);
printf("Enter your height in meters:\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%f", &meters);
BMI = kg / (meters * meters); // BMI metric formula.
printf("Your BMI is %.2f\n\n", BMI);
} else {
printf("Invalid input, please try again.\n\n");
goto start;
}
printf("Do you want to continue?(y/n)\n");
fgets(input, sizeof(input), stdin);
sscanf(input, "%c", &choice);
} while ((choice == 'y') || (choice == 'Y'));
printf("\n\nGood bye!\n\n");
return 0;
}
Hello guys. I need help with the code above. I'm not sure how to explain my problems but I will try to do it as clearly as possible.
First problem:
My first problem is how do I make an error announcement for say, line 19-21's input. For example, if a user inputs "abcdef" for the input command in line 21, I want the program to go "Invalid input, please try again." I've tried with if else but I can't seem to make it work. Any suggestions on my problem there?
Second problem:
My second problem is that, after I enter a valid input for the whole program and re-loop it back to the start, I find that my previous BMI value stays with the memory still when I enter invalid inputs such as "abcdef" when the program asks me to input my height and weight. How do I remove the previous value of the variable BMI when I re-loop the program back to the start?
I'm sorry if you guys are having problems understanding my question but I tried my best to explain as clearly as possible but nevertheless, please leave a comment if you are not sure of what I meant.
Thanks in advance for your time and I really appreciate any help I can get right now.