# n00b tackles another problem set, needs more help :\

This is a discussion on n00b tackles another problem set, needs more help :\ within the C Programming forums, part of the General Programming Boards category; Getting closer aren't I? Problem now is that all values are coming back "0" so the calculation is not working. ...

1. Getting closer aren't I? Problem now is that all values are coming back "0" so the calculation is not working.

help

Code:
```#include <stdio.h>
#include <cs50.h>

#define inchesPerFoot 12

int
main(int argc, char * argv[])
{

/* Collect data from user */

float pounds, feet, inches, totalHeight, weight, total;
printf("Enter your weight in pounds: ");
pounds = GetFloat();
printf("Enter your height in feet (i.e. if you are 6'4, enter 6: ");
feet = GetFloat();
printf("Enter your height in inches (i.e. if you are 6'4, enter 4: ");
inches = GetFloat();

/*We now know the user's pounds, feet, and inches. Now we need to get their
total height by putting together the feet and inches they entered */

totalHeight = (feet * inchesPerFoot) + inches;

/*Now we have their height and weight and we can calculate their BMI */

total=(weight / (totalHeight * totalHeight) * 703);

/* Now we will print out all the info we have gathered so far */

printf("Your weight was entered as: %d", weight);
printf("Your total height is: %d" , totalHeight);

/* Now we can tell them about their BMI */

if (total < 18.5)
printf("You are underweight\n");
else if (total <=24.9)
printf("You are normal weight\n");
else if (total <= 29.9)
printf("You are overweight\n");
else
printf("You are obese\n");

return 0;
}```

2. Try printing out "pounds" and "feet" and "inches" before using them in your calculation, to make sure they're being read correctly. Also, is your calculation actually correct? The parenthesis don't really do anything for you, since multiplication is handled before division anyway.

Quzah.

3. quzah, thanks. I did that and it shows all zeroes for values

4. Well then, your GetFloat function isn't working right. Go fix it.

Quzah.

5. For the record there was nothing wrong with the getFloat function. Here is the completed, working bmi program. I am certain there are easier ways to do this rather than spell everything out explicitly like I did, so I welcome the feedback. Either way, I am very proud of this first program I wrote!!

For posterity, if you are taking Harvard University's CS50 course, don't cheat and copy this. Even if you're struggling like crazy, just stick with it, and approach it problem by problem and sooner or later you will get it... like I did.. and if I can do this.. ANYONE can do it. I am happy to help anyone out who is struggling with this. Visit my web site and contact me: www.boleroinc.com

Thanks all!!

Mark

Code:
```#include <stdio.h>
#include <cs50.h>
#define inchesPerFoot 12.0

int
main(int argc, char * argv[])
{

/*First we must declare all the variables we will be using. "Float" tells the computer we will use real numbers.*/
/*We set pounds, feet, and inches to -1 for the negative # validation*/

float pounds=-1, feet=-1, inches=-1, height, bmiHeight, bmi;

/* Collect data from user */

do //here we use a DO loop so if negative numbers are entered, the program asks for the value again
{
printf("Enter your weight in pounds: ");
pounds = GetFloat();//here we call the GetFloat function that is in the cs50.h header
}
while (pounds <= 0);
do
{
printf("Enter your height in feet (i.e. if you are 6'4, enter 6: ");
feet = GetFloat();
}
while (feet <= 0);
do
{
printf("Enter your height in inches (i.e. if you are 6'4, enter 4: ");
inches = GetFloat();
}
while (inches <= 0);

/*We now know the user's pounds, feet, and inches. Now we need to get their
height by adding the feet and inches they entered */

height = (feet * inchesPerFoot) + inches;

/* Now we must square their height in inches to get their bmi height */

bmiHeight = (height * height);

/* Now we have everything we need to get their BMI  */

bmi = (pounds / bmiHeight) * 703.0;

/* Now we will print out all the info we have gathered so far */

printf("\nYour weight is: %.1f" , pounds);
printf("\nYour height is: %.1f" , height);
printf("\nYour BMI is: %.1f" , bmi);

/* and tell them their bmi status */

if (bmi < 18.5)
printf("\nYou are underweight");
else if (bmi <=24.9)
printf("\nYou are normal weight");
else if (bmi <= 29.9)
printf("\nYou are overweight\r");
else
printf("You are obese");

return 0;
}```
For the record, when I first approached this problem I was befuddled. I literally had no idea where to start. SO I started with something very simple. And from there I built it out piece by piece. This is what I started with.

#include <stdio.h>
#include <cs50.h>

int
main(int argc, char * argv[])
{
int weight, height, inches;
printf("Enter your weight in pounds: ");
weight = GetInt();
printf("Enter your height in feet: ");
height = GetInt();
printf("Enter your height in inches: ");
inches = GetInt();

printf("The sum of %d and %d and %d is %d!\n", weight, height, inches, weight + height + inches);

return 0;

}

6. It's not complete. It still fails on the last point:
As implied by the above, do require that users’ inputs be non-negative; rather than quit upon invalid input, let the user re-try again and again.
(Emphasis mine.)

7. Ahhh you are good Elysia I added loops now and it works!! I have updated the code in prev post

8. Well done. What could be improved is your indentation, if you want to be a good programmer.

9. Elysia, thanks for the feedback. I realize the indentation is bad. I will clean it up. Thanks for your help and insight

10. StrictlyC: Not sure why you are saying to print decimal places.
I don't see where printing any numbers is a requirement here at all.
All I need to do is print a string (i.e. "You are underweight") so I 'm not sure how your advice about printing decimals is relevant?

Underlined are some sample inputs. <<<<< opps my bad
Weight in pounds: 165
Height (feet): 6
Height (inches): 2
As for the loop, I have no idea where/how to put that in here considering any of the numbers entered could be negative. It seems like I would need 3 loops, one for each question that could potentially create a negative number--and that doesn't sound practical.
And again I will refer you to my responce

[CODE]

a for loop is like this
for(initialization; control; expression)
{
code that should be
contained inside the loop
}

a while loop is as this

while(control) (i.e wieght < 0)
{
code here
}

and the do while

do
{

code here

} while(control);
[\CODE]

If your not willing to try then give up and stop asking questions I even threw in the I.E. (wieght < 0)
And no you really don't need three seperate loops.

Code:
```while (condition1 < 0 && condition2 < 0 && condition3 < 0)
{
code here;
here;
as much as you want;
}```
I will not do your homework/learning assignment for you. Just point you in the right direction.
If your program has a divsion problem in it I'd worry more about division by zero than someone entering a negative number.

Your procress to get bmiHeight could have an answer of 0 if the correct values were entered into the other variables.

11. It would be annoying to have to re-enter correct information if you just entered something wrong, so a single loop is not something I would recommend.

12. Originally Posted by Elysia
It would be annoying to have to re-enter correct information if you just entered something wrong, so a single loop is not something I would recommend.
Well it would be better in my opinion. Also I would make it so they was a limit on max number of inches you could enter/ and max number of height (no ones over 8 foot are they? 8 foot 24 inches? How about weight? 12000 lbs? If your going to go through all the trouble of making sure the user doesn't enter a negative number cause it's not realistic then may as well. Also inside a loop with all three I would print the values back out and want comfimation all data enter was correct. Accidently entering weight for inches... or something similar It would be even worse having to enter two more things needlessly when you know the result is going to be wrong in the final outcome cause you did a typo at the first prompt.

How to enter 6 foot exactly in your coode? 5foot 12 inches?
also my way you lose a variable inches and feet. (you dont need as many as you still have)

Code:
``` do
{
printf("Enter your height in feet (i.e. if you are 6'4, enter 6: ");
height = GetFloat();                          <<<<<<<<<<<<<<<<<<<<<<< Changed

printf("Enter your height in inches (i.e. if you are 6'4, enter 4: ");
height = (height * inchesPerFoot) + GetFloat();
}
while (height <= 0);

completely doing away with this.

height = (feet * inchesPerFoot) + inches;```

13. Just for fun:

Code:
```printf("Your height is: %.0f feet and %d %s."
, (height/12)-.5
, (int)height-((((int) height)/12)*12)
, ((int)height-((((int) height)/12)*12)) != 1 ? "inches" : "inch"  );```

14. Thanks but way beyond me at this early stage

15. Originally Posted by rightbrainer
Thanks but way beyond me at this early stage
Well hope you at least addressed the problem of not being able to enter a even height
i.e 5 ft., 6 ft. Good luck.

Page 2 of 3 First 123 Last