# 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; Thanks for your patience with me. I know these messages are probably annoying. Thanks for bearing with me. So my ...

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

Thanks for your patience with me. I know these messages are probably annoying. Thanks for bearing with me.

So my new assignment is as follows:

(15 points.) According to the Center for Disease Control (CDC), Body Mass Index (BMI) is a
“reliable indicator of body fatness for most people and is used to screen for weight categories
that may lead to health problems.” An adult’s BMI is a function of his or her weight and
height, the formula for which is

w/h(square)x703

where w is weight in pounds and h is height in inches.

Adults’ weights can be categorized by
BMI per the table below.
BMI Status
< 18.5 Underweight
18.5 – 24.9 Normal
25.0 – 29.9 Overweight
> 29.9 Obese

Write, in a file called bmi.c, a program that first prompts users for their weight and height and then informs them of their BMI and status. Americans tend to think in terms of feet, so let users provide their height in feet plus inches. The aesthetics of your program are largely up to you, but your program must prompt users for input in this order: pounds then feet then inches. Functionally, then, your program must resemble the below. Underlined are some sample inputs.

Weight in pounds: 165
Height (feet): 6
Height (inches): 2
Your BMI is 21.2. You are normal.

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. Also round your program’s floating-point output to one decimal place.
I have read the lecture material repeatedly as well as the notes and I have done my very best with the knowledge I have so far. I have skipped the floating point part because I haven't a clue what to put. Same with the feet to inches conversion.

For the record this won't compile (surprise, I know). This is what I have come up with so far:

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

int
main(int argc, char * argv[])
{
int weight, height, inches, total;
printf("Enter your weight in pounds: ");
weight = GetInt();
printf("Enter your height in feet (i.e. if you are 6'4, enter 6: ");
height = GetInt();
printf("Enter your height in inches (i.e. if you are 6'4, enter 4: ");
inches = GetInt();

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

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

return 0;
}```
Again, I have read everything I can get my hands on before posting this.

Thank you kindly for your help.

PS: I will gladly take hints too. I am not looking to cut corners. I truly want to learn C. Feel free to make me think with some good questions !

2. Another case of "unnecessary checking":
Code:
```          if (total < 18.5)
printf("You are underweight\n");
else if (total >= 18.5 && total <=24.9)```
You are testing to see if the number is less than 18.5, then checking if it's greater or equal to 18.5 in the else-side of the if. How do you think you would get to that else if there was a value less than 18.5?

Also, I would write a function that takes a message string parameter, prints the message, reads an integer, and validates it (must not be negative), repeating the message.

Edit: And you may find that
Code:
`         total=(weight / height * height * 703);`
needs a cast to floating point type, as the values are integers, and weight / height will be an integer value, and a 200 lbs / 6ft (72") would produce 2, not 2.77.

--
Mats

3. So you need to create your GetInt function. Then I suggest that you take a look at floating point numbers (you must have some kind of course material you can refer to?). You need a floating-point type to store decimals.
Then the assignment says to check for negative numbers. Can't be difficult, can it? What are you having difficulty with?

4. I guess I'll hit on what others didn't. You have to use a format specifier in the printf() statement to have it display the correct number of decimal places.

printf("%.3f", BMI); would produce a floating point number with three decimal places. I truly don't believe you have looked at all other possible resources.
printf("%.5f", BMI); would print the value of BMI with 5 decimal places.
Doesn't seem that hard does it?

As for not quiting if a - number or invald input is entered. You need a loop.
Three types of loops exist(excluding goto which could achieve a looping process)

You have the for loop, the while loop, and the doWhile loop.
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);```
I only hit the basics really (giving you the names of what you need to be googling)
Good luck.

5. Hi all, and thanks for responding. Please understand, I am a complete beginner. The course is a complete beginner's course. I have zero programming experience. The instructor covered loops, floats, etc. But, he covered them one by one. He didn't show how to put them together to create a program that uses all of them in a sequential manner. The same can be said for the books and resources on the net. Yes, all the info is there, but putting them together is a different story.

Does anyone know where I could find a complete beginner's forum?

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? 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.

Elysia: No, the getInt function is already in the cs50.h header

Matsp: I am checking both numbers because these are ranges. See the instructions to the project. To answer your question: I would NOT get to that elseif if the value was less than 18.5, and I would not want to. That is the point.

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

int
main(int argc, char * argv[])
{
int weight, height, inches, total;
printf("Enter your weight in pounds: ");
weight = GetInt();
printf("Enter your height in feet (i.e. if you are 6'4, enter 6: ");
height = GetInt();
printf("Enter your height in inches (i.e. if you are 6'4, enter 4: ");
inches = GetInt();

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

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

return 0;
}```
Is there a complete beginner's forum, anywhere?

6. ## My new and improved code!

Ok granted it's still hosed, but with more research I think I improved it a lot.... or maybe not? I still need to figure out how to get the loop in here in case they enter a negative number.. and something about float to accommodate decimals? Errghghh. Help. Is this looking better?

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

#define inchesPerFoot 12

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

/* Collect data from user */

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

/*We now know the user's pounds, feet, and inches. Now we need to get the 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(2) * 703);

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

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

return 0;
}```

7. Originally Posted by matsp
Another case of "unnecessary checking":
Code:
```          if (total < 18.5)
printf("You are underweight\n");
else if (total >= 18.5 && total <=24.9)```
You are testing to see if the number is less than 18.5, then checking if it's greater or equal to 18.5 in the else-side of the if. How do you think you would get to that else if there was a value less than 18.5?
On the other hand, writing it this way shows a clear correspondance between the code and the spec. I don't think the explicitness is hurting anything.

8. Originally Posted by rightbrainer
Ok granted it's still hosed, but with more research I think I improved it a lot.... or maybe not? I still need to figure out how to get the loop in here in case they enter a negative number.. and something about float to accommodate decimals? Errghghh. Help. Is this looking better?
The far easiest way of doing programming the beginning is making a flow chart.
Write down your logic on a paper with arrows that point where to go next.
Then translate it to psuedo code and finally to real code. It's not difficult.
If you want to do it all by yourself, then that is the best kind of advice I can give you. Writing out logic in code is no different than your thinking pattern.
And I know of no function called GetInt.

9. Elysia, I already told you getInt is in the cs50 header I am including. Here it is for the record:

Code:
```int
GetInt()
{
// try to get an int from user
while (true)
{
// get line of text, returning INT_MAX on failure
string line = GetString();
if (line == NULL)
return INT_MAX;

// return an int if only an int (possibly with
// leading and/or trailing whitespace) was provided
int n; char c;
if (sscanf(line, " %d %c", &n, &c) == 1)
{
free(line);
return n;
}
else
{
free(line);
printf("Retry: ");
}
}
}```

10. When I try to compile this, I get an error. The error is "In function main, called object on line 27 is not a function" . Line 27 is: totalHeight = (feet * inchesPerFoot) + inches

Next error is "syntax error before printf on line 38. Line 38 is printf("You are obese\n");

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

#define inchesPerFoot 12

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

/* Collect data from user */

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

/*We now know the user's pounds, feet, and inches. Now we need to get the 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(2) * 703);

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

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

return 0;
}```

11. This:
Code:
`else (total > 29.9)`
should be:
Code:
`else`
After all, it is everything else.

12. An adult’s BMI is a function of his or her weight and
height, the formula for which is

w/h(square)x703
Also, check your formula carefully and understand why:
Code:
`total=(weight / height * height * 703);`
is not the same as:
Code:
`total=(weight / (height * height) * 703);`

13. hk.. thanks! It compiles and runs! Problem is now that no matter what values I enter it says "you are under weight"

I suspect this is because I am not handling decimal points..?? Problem is, I don't know how. Anything else that could be causing? Latest code:
Code:
```#include <stdio.h>
#include <cs50.h>

#define inchesPerFoot 12

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

/* Collect data from user */

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

/*We now know the user's pounds, feet, and inches. Now we need to get the 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 can tell them about their BMI */

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

return 0;
}```

14. total will still be an integer value here, so it's unlikely to be 18.5 or 29.9.

--
Mats

15. Ok now I get that part... so I need to change all INT references to FLOAT (there is also a getFloat function in the cs50.h header). Is this right? What else is now wrong?

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 can tell them about their BMI */

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

return 0;
}```

Page 1 of 3 123 Last
Popular pages Recent additions