Thread: Need help...

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    3

    Need help...

    Hi! I really need help on my project which asks the user his height and weight. The output should be 2 exercises that he/she should do and in how many days(the code should give the time) to get to an ideal weight. I'm really desperate as this is due tomorrow. Can anyone help me or give me tips on how I can finish this code? mp2.c
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly are the problems that you are facing with the code that you have written?
    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

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    3
    I don't really understand how to get the exercises and the timespan it has to be done on. We were given 12 exercises with specific METs and from that, the code should calculate how many days will the user have to exercise, which exercises are going to be done, and with the 2 exercises picked how many minutes it's supposed to be done each day.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you can't explain what's wrong, you will get "works for me" and random guesses as responses.

    I ran the program a couple of times.

    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./weight
    Input your height in feet and inches respectively: 5 2
    Input your weight in pounds: 134
    Are you (M)ale or (F)emale? m
    Your BMI(Body Mass Index) is 24.6.
    You are of Normal Weight
    Keep up what you are doing! Don't forget to exercise and eat healthy.
    The process will take 2674 days
    Would you like to run the program again? (Y)es or (N)o: n
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./weight
    Input your height in feet and inches respectively: 6 4
    Input your weight in pounds: 212
    Are you (M)ale or (F)emale? m
    Your BMI(Body Mass Index) is 25.9.
    You are Overweight
    Your ideal weight is 185 pounds.
    You need to burn 94500 calories to achieve desired weight.
    The process will take 94 days
    Would you like to run the program again? (Y)es or (N)o: n
    What are you typing in?

    It seems like with normal input everything works. I did see some warnings about uninitialized variables after compiling, but other than that it works for me.

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    3
    Need help...-mp-jpg

    I'm just missing the exercise and MET part and I don't really know how to code it. The program doesn't have errors, I just need to know how to do the exercise and MET part.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A lot of people on here are not happy to follow links.

    You should post your code with code tags like this:
    Code:
    #include <stdio.h>
    
    
    void getHeight(float *ft, float *in, float *meters, float sum) /*To get the user's height in feet and inches and convert it to meters*/
    {
        printf("Input your height in feet and inches respectively: ");
        scanf("%f%f", &*ft, &*in);
        
        sum = *ft + *in / 12;
        *meters = sum * 0.3048;
    }
    
    
    void getWeight(float *lb, float *kg) /*To get the user's weight in pounds and convert it to kilograms*/    
    {
        printf("Input your weight in pounds: ");
        scanf("%f", &*lb);
        
        *kg = *lb / 2.2;
    }
    
    
    void getBMI(float kg, float meters, float *bmi) /*To compute for the user's BMI(Body Mass Index) using his/her height and weight*/
    {
        *bmi = kg / (meters * meters);
    }
    
    
    void getGender(char *gender) /*To get the user's gender which will be used in computing for the ideal weight*/
    {
        printf("Are you (M)ale or (F)emale? ");
        scanf(" %c", &*gender);
    
        while(*gender != 'M' && *gender != 'F' && *gender != 'm' && *gender != 'f')
        {
            printf("Wrong input. Please input either M for Male or F for Female: ");
            scanf(" %c", &*gender);
            
        }
            
    }
    
        
    void getWeightCategory(float bmi, int *category) /*To get the user's weight category using the computed BMI*/
    {
        if(bmi < 18.5)
            *category = 1;
        
        else if(bmi >= 18.5 && bmi <= 24.9)
            *category = 2;
        
        else if(bmi >= 25 && bmi <= 29.9)
            *category = 3;
        
        else
            *category = 4;
    }
    
    
    void getIdealWeight(int *ideal, float ft, float in, char gender, int category) /*To get the user's ideal weight*/
    {
        ft = ft * 12;
        in = ft + in - 60;
        
        if(gender == 'M' || gender == 'm')
            {
            *ideal = 105;
            
            if(ft >= 60)
                *ideal = 105 + in * 5;
            }     
            
        else if(gender == 'F' || gender == 'f')    
            {
            *ideal = 95;
            
            if(ft >= 60)
                *ideal = 95 + in * 4;
            }
    }
    
    
    void getCaloriesNeeded(float pounds, int ideal, int *calneeded, int category)/*To get the calories needed to burn*/
    {
        if(category == 3 || category == 4)
            *calneeded = (pounds - ideal) * 3500;
    }
    
    
    void getDays(int calneeded, int *days)/*To get the time needed to do the process*/
    {
        *days = calneeded / 1000;
        printf("The process will take %d days\n", *days);
    }
    
    
    void displayResult(int category, int ideal, float bmi, int *gain, float pounds, int calneeded) /*To display the weight category, ideal weight, bmi, and calories needed to burn*/
    {
        printf("Your BMI(Body Mass Index) is %.1f.\n", bmi);
        
        *gain = ideal - pounds;
        
        if(category == 1)
            printf("You are Underweight\nYou still need to exercise. Swimming, brisk walking, golf, or weight lifting should be good exercises for you.\nYour ideal weight is %d pounds.\nYou need to gain %d pounds.\n", ideal, *gain );
        else if(category == 2)
            printf("You are of Normal Weight\nKeep up what you are doing! Don't forget to exercise and eat healthy.\n");
        else if(category == 3)
            printf("You are Overweight\nYour ideal weight is %d pounds.\nYou need to burn %d calories to achieve desired weight.\n", ideal, calneeded);
        else if(category == 4)
            printf("You are Obese\nYour ideal weight is %d pounds.\nYou need to burn %d calories to achieve desired weight.\n", ideal, calneeded);
    }
        
    
    void getResponse(char *response) /*To know if the user wants the program to run again*/
    {
        printf("Would you like to run the program again? (Y)es or (N)o: ");
        scanf(" %c", &*response);
        
        while(*response != 'Y' && *response != 'N' && *response != 'y' && *response != 'n')
        {
            printf("Wrong input. Please input either Y for Yes or N for No: ");
            scanf(" %c", &*response);
        }
    }
        
            
    int main() /*Calls all the functions inside*/
    {
        /*Variables used to call the functions*/
        int ideal, category, gain, calweight, calneeded, days, met1[12] = {12.3, 7.8, 7.2, 7.0, 6.8, 6.5, 6.0, 4.8, 4.3, 3.8, 2.3, 2.3}, met2[12] = {12.3, 7.8, 7.2, 7.0, 6.8, 6.5, 6.0, 4.8, 4.3, 3.8, 2.3, 2.3};
        char gender, response;
        float feet, inches, pounds, bmi, kg, meters, sum;
        
        do
        {
            getHeight(&feet, &inches, &meters, sum);
            
            getWeight(&pounds, &kg);
    
            getGender(&gender);
    
            getBMI(kg, meters, &bmi);
    
            getWeightCategory(bmi, &category);
        
            getIdealWeight(&ideal, feet, inches, gender, category);
            
            getCaloriesNeeded(pounds, ideal, &calneeded, category);
    
            displayResult(category, ideal, bmi, &gain, pounds, calneeded);
            
            getDays(calneeded, &days);
            
            getResponse(&response);
    
        }
        while(response == 'Y' || response == 'y'); /*The do-while statement inside int main() is used to ask if the user wants to run the program again (see function getResponse)*/
        
        return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Rewrite your code in testable way. For example:

    Code:
    float getHeight(const float ft, const float in)
    {
        return 0.3048 * ft + 0.0254 * in;
    }
    Pure function - Wikipedia, the free encyclopedia
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

Popular pages Recent additions subscribe to a feed