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. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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);
             printf("Your BMI is: %d", total);
             
    /* 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. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    quzah, thanks. I did that and it shows all zeroes for values

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well then, your GetFloat function isn't working right. Go fix it.

    Quzah.
    Hope is the first step on the road to disappointment.

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

    }
    Last edited by rightbrainer; 04-17-2009 at 02:05 PM.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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.)
    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.

  7. #22
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Ahhh you are good Elysia I added loops now and it works!! I have updated the code in prev post

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Well done. What could be improved is your indentation, if you want to be a good programmer.
    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. #24
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Elysia, thanks for the feedback. I realize the indentation is bad. I will clean it up. Thanks for your help and insight

  10. #25
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    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?
    Your original post:


    Underlined are some sample inputs. <<<<< opps my bad
    Weight in pounds: 165
    Height (feet): 6
    Height (inches): 2
    Your BMI is 21.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.
    Last edited by strictlyC; 04-17-2009 at 06:11 PM.

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    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.
    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.

  12. #27
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by Elysia View Post
    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;
    Last edited by strictlyC; 04-17-2009 at 06:41 PM.

  13. #28
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    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. #29
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    Thanks but way beyond me at this early stage

  15. #30
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by rightbrainer View Post
    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 FirstFirst 123 LastLast
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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21