Thread: More complex Function

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    30

    More complex Function

    Ok, My question for this one is a bit more complicated. What I am trying to do is to get the program to first calculate the Basal Metabolic Rate, then the amount of calories needed to maintain the current weight, and lastly convert it. It compiles, but I believe I established my functions wrong because they do not seem to calculate properly. Any suggestions?

    Code:
    #include <iostream>#include <cmath>
    
    
    using namespace std;
    
    
    float metabolic_rate;
    // Returns the metabolic_rate as a floating point value.
    float digestion_energy;
    // Returns the digestion_energy as a floating point value.
    
    
    int main ()
    
    
    {
    
    
        int P, intensity, minutes, calories, servings;
        double weight = P;
        double metabolic_rate = 70 * pow ( P / 2.2 , 0.756 );
        double activity = 0.0385 * intensity * P * minutes;
        double digestion_energy = calories * .01;
    
    
        {
    
    
        cout << "Welcome to the Basal Metabolic Rate program.";
        cout << endl;
    
    
        cout << "First we need your weight. Please enter your weight: ";
        cin >> weight;
        cout << endl;
    
    
        cout << "Please enter the intensity of your workout: ";
        cin >> intensity;
        cout << endl;
    
    
        cout << "Please enter the duration (in minutes) that you worked out: ";
        cin >> minutes;
        cout << endl;
    
    
        cout << "Please enter the calories you consumed: ";
        cin >> calories;
        cout << endl;
    
    
            {
    
    
            cout << "We will first find the ammount of calories to start your Basal Metabolic Rate." <<endl;
            cout << "Your Basal Metabolic Rate is: ";
            cout << metabolic_rate << endl;
            cout << endl;
    
    
            }
                {
    
    
                cout << "Next we will calculate the calories required for your activity." << endl;
                cout << "The total ammoount of calories you need is: ";
                cout << digestion_energy;
                cout << endl;
    
    
                }
    
    
                    {
                        cout << "Lastly we will convert the calories you need into servings.";
                        cout << "You must have a total of: ";
                        cout << servings;
                        cout << " in order to maintain your current weight.";
    
    
                    }
    
    
        }
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Well to start you do not have any function at all except main() which is mandatory.

    Then, you use P while it has not been assigned any value and the same goes for intensity and minutes and calories. You cannot make calculations with empty variables.

    You also have two include directives on one line. I'm not such if the linker will understand it but you should not do it anyway. One instruction - one line.

    And finally... why the curly braces ? You have them all over the place for no reason. You only need curly braces when entering a new logical segment of your code (inside of a function, inside of a class, inside of if/else/while/for statement, etc.)

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Ok, this is my new code. Any suggestions?

    Code:
    #include <iostream>#include <cmath>
    
    
    using namespace std;
    
    
    double basal_rate ( int weight_par, int calories );
    double physical_activity( int intensity_par, int minutes_par, int calories_par );
    
    
    int main ()
    
    
    {
    
    
        int P;
        double intensity, minutes, calories;
    
    
        {
    
    
        cout << "Welcome to the Basal Metabolic Rate program.";
        cout << endl;
    
    
        cout << "First we need your weight. Please enter your weight: ";
        cin >> P;
        cout << endl;
    
    
        cout << "Please enter the intensity of your workout: ";
        cin >> intensity;
        cout << endl;
    
    
        cout << "Please enter the duration (in minutes) that you worked out: ";
        cin >> minutes;
        cout << endl;
    
    
        cout << "Please enter the calories you consumed: ";
        cin >> calories;
        cout << endl;
    
    
        double physical_activity = 0.0385 * intensity * P * minutes;
        double digestion_energy = calories * .01;
        double basal_rate = 70 * pow ( P / 2.2 , 0.756);
    
    
        cout << "We will first find the ammount of calories to start your Basal Metabolic Rate." << endl;
        cout << "Your Basal Metabolic Rate is: ";
        cout << basal_rate;
        cout << endl;
    
    
        cout << "Next we will calculate the calories required for your activity." << endl;
        cout << "The total ammoount of calories you need is: ";
        cout << physical_activity;
        cout << endl;
    
    
        int serving_size = 1 + calories;
    
    
        cout << "The serving size you need to consume daily is ";
        cout << serving_size;
        cout << " in order to maintain your current weight." << endl;
    
    
        }
    
    
    return 0;
    
    
    }
    
    
    double basal_rate ( int weight_par, int calories_par )
    
    
    {
        return ( calories_par, weight_par);
    }
    
    
    double physical_activity( int intensity_par, int minutes_par, int calories_par )
    
    
    {
        return ( intensity_par, minutes_par, calories_par );
    }
    Last edited by iGuardian; 09-24-2011 at 09:52 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You have some serious knowledge deficiencies. Read through our C++ Made Easy Tutorials, specifically pay attention to the lesson on functions. Think about your homework, and then come up with a solution. It appears we are all just wasting our time with you.

    BTW, posting BS code and asking for help is like posting no code at all. You have clearly not paid any attention to the advice that has already been given. Stop wasting our time with this until you are willing to learn.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    30
    Thanks for the feedback!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>#include <iostream>#include <cmath>
    Still wrong.

    >>return ( intensity_par, minutes_par, calories_par );
    Will not do what you want. What do you want it to do?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complex Structures
    By JJ007 in forum C Programming
    Replies: 3
    Last Post: 10-27-2009, 09:47 AM
  2. Complex Cholesky Function
    By magda3227 in forum C Programming
    Replies: 3
    Last Post: 07-18-2008, 08:55 PM
  3. how complex can you do?
    By wart101 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2005, 11:50 AM
  4. complex.h in g++ not working
    By amolh in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2005, 01:46 AM
  5. more complex C++
    By EvBladeRunnervE in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 06:07 PM