Can someone check my coding? BMI Calc.

May 27, 2011 at 19:04:46
Specs: Windows 7
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;
}


See More: Can someone check my coding? BMI Calc.

Report •


#1
May 27, 2011 at 19:35:38
<deleted by nails>

Report •

#2
May 27, 2011 at 20:17:27
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


Report •

#3
May 27, 2011 at 21:49:00
<deleted by nails>

Report •

Related Solutions

#4
May 28, 2011 at 01:07:07
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.)


Report •

#5
May 28, 2011 at 01:11:05
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.


Report •

#6
May 28, 2011 at 08:01:47
Apologies on my part. this is one thread I should have checked closer.

Report •

#7
May 28, 2011 at 10:31:23
Got it.
Thanks. 1st time programmer working on something late at night doesn't work so great!

Report •

Ask Question