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

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Last edited by matsp; 04-16-2009 at 02:56 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    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.
    Last edited by strictlyC; 04-16-2009 at 09:51 AM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    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. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rightbrainer View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    else (total > 29.9)
    should be:
    Code:
    else
    After all, it is everything else.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    total will still be an integer value here, so it's unlikely to be 18.5 or 29.9.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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;
             }
    Last edited by rightbrainer; 04-16-2009 at 01:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. class, set, < problem
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2003, 09:25 AM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM