#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", &pounds);
		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.

First problem: Try something like

do {
    printf("Enter you weight in pounds:\n");
    fgets(input, sizeof(input), stdin);
} while (sscanf(input, "%f", &pounds) == 0);

Second problem: Put BMI = 0; between lines 46 and 47.

Thanks for the solution, but can you kindly explain to me what you did there in the first solution? Especially line 4

(sscanf(input, "%f", &pounds) == 0);

What does == 0 do?

EDIT:

I tried your first solution and it only worked for the pounds part of the input but not the inches part. When I input a single character when prompted, it still calculates the equation rather than re-requesting a valid input. Any idea..? I did create a loop for inches as well like you suggested for pounds.

Edited 5 Years Ago by yassop: n/a

sscanf returns the number of items it converted successfully, so what line 4 is checking is how many numbers did you get (should be 0 or 1 since you only specify one format option). If it is 0 (the == 0 part) then the user didn't enter a valid number, so we need to ask again (do/while continues as long as the while part is true).

Actually that was my bad. I realised that I had executed the wrong .exe file from the current source code one. Everything is working as intended now. Thanks alot for your explanation and help =).

This question has already been answered. Start a new discussion instead.