I'm trying to create a BMI calculator in C programming! I just can't get it to calculate the final result. Everything else runs fine. //Katrina

//5/27/11

//Assignment 1- Part A: BMI Calculator#include <stdio.h>

int main() {

int inches, pounds;

float BMI;

printf("What is your height in inches? \n", inches);

scanf("%d", & inches);

printf("What is your weight in pounds? \n", pounds);

scanf("%d", & pounds);

BMI=((pounds*703)/(inches*inches));

printf("Your BMI is: %.2f \n", BMI);

system:("pause");

return 0;

}

<deleted by nails>

No that's didn't help either. The formula the prof. gives us is BMI=(weight in pounds*703)/(height in inches)^(2) I've tried everything so many different combinations

What's happening is the Output will let me type the height and the weight

and after I press enter after inputting weight the screen will flash for a fraction of a second BMI= (something unreadable)

and then the screen will close.

AAAHHHH

<deleted by nails>

As well as the simple mechanics of the pause and the question over the formula, there is a classic C error in your program. You divide an int by an int and assign the result to a float: BMI=((pounds*703)/(inches*inches));

This will work, due to automatic type coercion, but will give an incorrect result - you will lose the fractional part of the result. The int will be divided by the int and produce an int result, which is then co-erced to a float. For example the code:

int i= 3, j = 4;

float r;r = j/i;

produces the result r = 1.0000..., not r = 1.3333....

You need to cast the ints to floats (or just make them floats in the first place) before doing the division:

BMI=((float)(pounds*703)/(float)(inches*inches));

(Strictly speaking, the first cast would suffice, but it's more elegant to cast both the numerator and the denominator.)

Nails, Unless I'm being really stupid, aren't:

BMI=(pounds/(inches*inches)) * 703;

and

BMI=((pounds*703)/(inches*inches));

exactly the same thing (forgetting for the moment questions of type co-ercion)? As mathematical formulae they are equivalent.

Apologies on my part. this is one thread I should have checked closer.

Got it.

Thanks. 1st time programmer working on something late at night doesn't work so great!

Ask Your Question

Weekly Poll

Do you think Microsoft's new Surface Go will be a hit?

Discuss in The Lounge

Poll History