Trouble with functions

This is a discussion on Trouble with functions within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that calculates the user's BMR, and finally fixed all the compiler errors, but ran into ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    Trouble with functions

    I am writing a program that calculates the user's BMR, and finally fixed all the compiler errors, but ran into a problem when executing the program. The the function "increase_bmr (string activity_lvl, double sum1)" is supposed to multiply the users base BMR considering how active they are on a daily basis. However the program outputs their base BMR (sum1), not the one that it should be outputting (sum2) which is the one that is multiplied by set percents to adjust for their activity levels. I'm fairly new to c++ and am stumped, can someone help me out here?

    Example of output:

    Enter the height in inches --> 65
    Enter the weight in pounds --> 125
    Enter the age --> 52
    Enter the sex (M for male and F for female) --> f
    Enter the activity level

    Sedentary Somewhat Active Highly --> active
    A Female with the weight 125 pounds, height 65 inches 52 years of age and with active activity level has a BMR of 1253.60. (should be 1755.04, not 1253.60)

    Another y/n -->

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void getInput (int& weight, int& height, int& age, char& sex, string& activity_lvl);
    double calculate_bmr (int weight, int height, int age, char sex);
    double increase_bmr (string activity_lvl, double sum1);
    void outputResults (double sum2, int weight, int height, int age, string full_sex, string activity_lvl);
    
    int main()
    
    {
    
      int height;
      int weight;
      int age;
      char sex;
      string activity_lvl;
      string full_sex;
      char again = 'Y';
      double sum1;
      double sum2;
    
      while (again == 'Y' || again == 'y')
    
      {
    
        getInput (weight, height, age, sex, activity_lvl);
        calculate_bmr (weight, height, age, sex);
    
        sum1 = calculate_bmr (weight, height, age, sex);
    
        increase_bmr (activity_lvl, sum1);
    
        sum2 = increase_bmr (activity_lvl, sum2);
    
        outputResults (sum2, weight, height, age, full_sex, activity_lvl);
    
        cout << "Another y/n -->    ";
        cin >> again;
    
        if (again != 'Y' && again != 'y' && again != 'N' && again != 'n')
        {
    
          cout << "Please enter a valid response:  Another y/n -->  ";
          cin >> again;
    
        }
    
      }
    
    return (0);
    
    }
    
    void getInput (int& weight, int& height, int& age, char& sex, string& full_sex, string& activity_lvl)
    
    {
    
      using namespace std;
    
      cout << "Enter the height in inches -->       ";
      cin >> height;
      cout << "Enter the weight in pounds -->       ";
      cin >> weight;
      cout << "Enter the age -->    ";
      cin >> age;
      cout << "Enter the sex (M for male and F for female) -->      ";
      cin >> sex;
    
      while (sex != 'm' && sex != 'M' && sex != 'f' && sex != 'F')
    
      {
        cout << "Please enter a valid sex (M for male and F for female) --> ";
        cin >> sex;
    
      }
    
      if (sex == 'M' || sex == 'm')
        full_sex = "Male";
      else if (sex == 'F' || sex == 'f')
        full_sex = "Female";
    
      cout << "Enter the activity level"
           << endl << endl
           << "     Sedentary   Somewhat   Active   Highly  -->     ";
      cin >> activity_lvl;
    
      while (activity_lvl != "Sedentary" && activity_lvl != "sedentary" &&
             activity_lvl != "Somewhat" && activity_lvl != "somewhat" &&
             activity_lvl != "Active" && activity_lvl != "active" &&
             activity_lvl != "Highly" && activity_lvl != "highly")
    
      {
    
        cout << "Please enter a valid activity level"
             << endl << endl
             << "   Sedentary   Somewhat   Active    Highly -->     ";
        cin >> activity_lvl;
    
      }
    
      return;
    
    }
    
    double calculate_bmr (int weight, int height, int age, char sex)
    
    {
    
      double sum1 = 0;
    
      if (sex == 'F' || sex == 'f')
        sum1 = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
      else if (sex == 'M' || sex == 'm')
        sum1 = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
    
      return (sum1);
    
    }
    
    double increase_bmr (string activity_lvl, double sum1)
    
    {
    
      double sum2 = 0;
    
      if (activity_lvl == "Sedentary" || activity_lvl == "sedentary")
        sum2 = sum1 * 1.2;
      if (activity_lvl == "Somewhat" || activity_lvl == "somewhat")
        sum2 = sum1 * 1.3;
      if (activity_lvl == "Active" || activity_lvl == "active")
        sum2 = sum1 * 1.4;
      if (activity_lvl == "Highly" || activity_lvl == "highly")
        sum2 = sum1 * 1.5;
    
      return (sum2);
    
    }
    
    void outputResults (double sum2, int weight, int height, int age, string full_sex, string activity_lvl)
    
    {
    
      using namespace std;
    
      cout << "A " << full_sex << " with the weight "
           << weight << " pounds, height " << height
           << " inches " << age << " years of age and with "
           << activity_lvl << " activity level has a BMR of "
           << sum2 << "." << endl << endl;
    
      return;
    
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    I don't think the type conversions between double and int are helping you. I would stick with doubles since the BMR equations require decimal precision. Nice and simple. It worked alright when I made sure that all the terms were doubles.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Quote Originally Posted by whiteflags View Post
    I don't think the type conversions between double and int are helping you. I would stick with doubles since the BMR equations require decimal precision. Nice and simple. It worked alright when I made sure that all the terms were doubles.
    what double and what int? i'm a little confused here

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    Well the height, weight and age of a person are all part of the BMR calculation. I think that your calculations are messed up because you are using integer for these numbers. There is no reason for them to be integers anyway... someone can be 70.5 inches tall and weigh 140.5 pounds or whatever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with functions
    By iamtazb in forum C Programming
    Replies: 3
    Last Post: 07-15-2012, 10:13 PM
  2. having trouble with functions
    By idfjvafnv in forum C Programming
    Replies: 12
    Last Post: 10-07-2011, 10:03 AM
  3. more trouble with functions
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2003, 03:04 AM
  4. trouble with functions
    By volk in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2003, 02:35 PM
  5. trouble with functions with &
    By diaperdandy in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 09:05 PM

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