Thread: Don't know why I'm getting this error?

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

    Question Don't know why I'm getting this error?

    Working on a program that calculates the users BMR and I ran into an error that I can't figure out how to fix. Before this error, I had an error I was getting in "function double increase_bmr (string activity_lvl, double sum1)" for the if statements that said that "sum1" was not declared, so I put "double sum1" into the function declaration and heading, as well as "sum1" in the function call, it fixed the "no sum1 declaration" but then I got this error:

    /var/tmp//cc066PnW.o: In function `main':
    proj3.cc: (.text+0x54c): undefined reference to `calculate_bmr(int, int, int, char)'
    collect2: ld returned 1 exit status

    I kinda understand what the error is trying to say, but I have no idea what I need to change to fix it, help?

    CODE:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    double calculate_bmr (int weight, int height, int age, char sex);
    double increase_bmr (string activity_lvl, double sum1);
    
    int main()
    {
    
      double choc_bar;      //number of chocolate bars the user can eat to maintain their weight
      double bmr;           //users BMR before taking into account the users activity level
      double mod_bmr;       //users BMR after taking into account the users activity level
      double sum1;          //returned value from function
      int height;           //users height in inches
      int weight;           //users weight in pounds
      int age;              //users age in years
      char sex;             //users sex male or female
      char again;           //determines if users wants data for another BMR or if they are done
      string activity_lvl;  //users activity level
      string full_sex;      //outputs the users sex in a full word male or female not just a character m or f
    
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout.precision(2);            //sets decimal space limit to 2 spaces
    
      while (again == 'Y' || again == 'y')
      {
    
        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')        //will reprompt the user if an incalid sex is entered
        {
          cout << "Please enter a vaild sex (M for male and F for female) -->       ";
          cin >> sex;
        }                                                                   //end reprompt
    
        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" &&        //will reprompt the user if an invalid activity level is entered
               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;
    
        }                                                                           //end reprompt
    
        bmr = calculate_bmr (weight, height, age, sex);
        mod_bmr = increase_bmr (activity_lvl, sum1);
    
        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 ";
    
        cout << "Another y/n -->    ";      //determines whether or not the user wants to do another BMR calculation if not program ends
        cin  >> again;
    
        return (0);
    
      }
    }
    
    double caluclate_bmr (int weight, int height, int age, char sex)        //calculate BMR
    
    {
    
      double sum1;
    
      if (sex == 'M' || sex == 'm')
        sum1 = 655 + (4.3 * weight) + (4.7 * height) - (6.8 * age);
      else if (sex == 'F' || sex == 'f')
        sum1 = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
    
      return (sum1);
    
    }
    
    double increase_bmr (string activity_lvl, double sum1)  //takes into account activity level and increases BMR
    
    {
    
      double sum2;
    
      if (activity_lvl == "Sedentary" || activity_lvl == "sedentary")
        sum2 = sum1 * 1.2;
      else if (activity_lvl == "Somewhat" || activity_lvl == "somewhat")
        sum2 = sum1 * 1.3;
      else if (activity_lvl == "Active" || activity_lvl == "active")
        sum2 = sum1 * 1.4;
      else if (activity_lvl == "Highly" || activity_lvl == "highly")
        sum2 = sum1 * 1.5;
    
      return (sum2);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    double caluclate_bmr (int weight, int height, int age, char sex) //calculate BMR
    Check your spelling.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Quote Originally Posted by Salem View Post
    double caluclate_bmr (int weight, int height, int age, char sex) //calculate BMR
    Check your spelling.
    yup, that did it... but now I have got a new problem. When I hit "a.out" it ends the program before anything is output..

    ex:

    g++ proj3.cc
    p2% a.out
    p2% a.out
    p2% a.out
    p2%

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The first while that is in your main has two conditions,but they can not be true!Why?Because again has not received a value!Maybe you should switch to a do..while loop ,or give a value to again that makes one condition true(e.g. value='y'.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Quote Originally Posted by std10093 View Post
    The first while that is in your main has two conditions,but they can not be true!Why?Because again has not received a value!Maybe you should switch to a do..while loop ,or give a value to again that makes one condition true(e.g. value='y'.
    the funny thing too is I had realized that walking from one class to another and completely forgot about it, haha. thanks for the both of you that helped me out here... two fairly easy to figure out problems... both of which slipped my attention every time I looked through the code, haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM