i have a problem with my code and i want you to fix it.......the code generate t
ID: 3554264 • Letter: I
Question
i have a problem with my code and i want you to fix it.......the code generate the program with no problem except that it gives wrong values for BMI ...........my code is:
#include <iostream>
#include<ctime>
#include<math.h>
using namespace std;
class HealthProfile
{
public:
HealthProfile(string, string, int,int,int,char,int,int);//constructor that initializes accountBalance
void getTargetHeartRate(int,double&,double&); //functionthat retrieves the Target Rate for the person
int getAge(); //function that retrieves the age of the person basedon
int getMaximumHeartRate(int ); //function that retrieves themaximum
string getlastName(); //function that retrieves the last name
string getfirstName(); //function that retrieves the first name
char getGender();
int getHeight();
int getWeight();
private:
string firstName; //first name for person
string lastName; //last name for person
char gender;
int age;
int height;
int weight;
int month; //month of birth for person
int day; //day of birth for person
int year; //year of birth for person
}; //end class HealthProfile
//constructor initializes firstName, lastName, and dateofBirth
HealthProfile::HealthProfile(string first, string last, int m,int d,int y,char g,int h,int w)
{
firstName = first;
lastName = last;
gender=g;
height=h;
weight=w;
month=m;
day=d;
year=y;
} //end HealthProfile constructor
string HealthProfile::getlastName()
{return lastName;
}
int HealthProfile::getHeight()
{return height;
}
int HealthProfile::getWeight()
{return weight;
}
char HealthProfile::getGender()
{return gender;
}
string HealthProfile::getfirstName()
{return firstName;
}
void HealthProfile::getTargetHeartRate(int m,double&a,double& b)
{a=m*.5;
b=m*.85;
//return TargetHeart; //gives the value of the target heart ratebased on the age of the person
}//end function getTargetHeartRate
int HealthProfile:: getAge() //function that retrieves the age ofthe person based on
{int age,m,d,y;
cout<<" Enter todays date: ";
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
age=y-year;
if(m<month)
age--;
else if(m==month)
if(d<day)
age--;
return age;
}
int HealthProfile:: getMaximumHeartRate(int a) //function thatretrieves the maximum
{return 220-a;
}
int main()
{int m,d,y,target,max,h,w,age;
char g;
double a,b;
string first,last;
cout<<"Enter first name: ";
cin>>first;
cout<<"Enter last name: ";
cin>>last;
cout<<"Enter gender(m/f):";
cin>>g;
cout<<"Enter height in inches: ";
cin>>h;
cout<<"Enter weight: ";
cin>>w;
cout<<"Enter Birthday: ";
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
HealthProfile person(first,last,m,d,y,g,h,w);
age=person.getAge();
max=person.getMaximumHeartRate(age);
cout<<person.getfirstName()<<""<<person.getlastName()<<": ";
cout<<"Gender:"<<person.getGender()<<" age:"<<age<<endl;
h=person.getHeight();
cout<<"Height: "<<h<<" inches ";
w=person.getWeight();
cout<<"Weight: "<<w<<" pounds ";
cout<<"Maximum heart rate: "<<max<<endl;
person.getTargetHeartRate(max,a,b);
cout<<"Target heart rate range between: "<<a<<"and "<<b<<endl;
cout<<"BMI= "<<(w*703)/pow(h,2)<<endl;
return 0;
}
Explanation / Answer
please rate - thanks
you are doing integer arithmetic to calculate the BMI. you need to do real arithmetic
change this instruction
cout<<"BMI= "<<(w*703)/pow(h,2)<<endl;
to
cout<<"BMI= "<<(w*703.)/pow(h,2)<<endl;
note by adding the decimal point to 703, real arithmetic is done
#include <iostream>
#include<ctime>
#include<math.h>
using namespace std;
class HealthProfile
{
public:
HealthProfile(string, string, int,int,int,char,int,int);//constructor that initializes accountBalance
void getTargetHeartRate(int,double&,double&); //functionthat retrieves the Target Rate for the person
int getAge(); //function that retrieves the age of the person basedon
int getMaximumHeartRate(int ); //function that retrieves themaximum
string getlastName(); //function that retrieves the last name
string getfirstName(); //function that retrieves the first name
char getGender();
int getHeight();
int getWeight();
private:
string firstName; //first name for person
string lastName; //last name for person
char gender;
int age;
int height;
int weight;
int month; //month of birth for person
int day; //day of birth for person
int year; //year of birth for person
}; //end class HealthProfile
//constructor initializes firstName, lastName, and dateofBirth
HealthProfile::HealthProfile(string first, string last, int m,int d,int y,char g,int h,int w)
{
firstName = first;
lastName = last;
gender=g;
height=h;
weight=w;
month=m;
day=d;
year=y;
} //end HealthProfile constructor
string HealthProfile::getlastName()
{return lastName;
}
int HealthProfile::getHeight()
{return height;
}
int HealthProfile::getWeight()
{return weight;
}
char HealthProfile::getGender()
{return gender;
}
string HealthProfile::getfirstName()
{return firstName;
}
void HealthProfile::getTargetHeartRate(int m,double&a,double& b)
{a=m*.5;
b=m*.85;
//return TargetHeart; //gives the value of the target heart ratebased on the age of the person
}//end function getTargetHeartRate
int HealthProfile:: getAge() //function that retrieves the age ofthe person based on
{int age,m,d,y;
cout<<" Enter todays date: ";
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
age=y-year;
if(m<month)
age--;
else if(m==month)
if(d<day)
age--;
return age;
}
int HealthProfile:: getMaximumHeartRate(int a) //function thatretrieves the maximum
{return 220-a;
}
int main()
{int m,d,y,target,max,h,w,age;
char g;
double a,b;
string first,last;
cout<<"Enter first name: ";
cin>>first;
cout<<"Enter last name: ";
cin>>last;
cout<<"Enter gender(m/f):";
cin>>g;
cout<<"Enter height in inches: ";
cin>>h;
cout<<"Enter weight: ";
cin>>w;
cout<<"Enter Birthday: ";
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
HealthProfile person(first,last,m,d,y,g,h,w);
age=person.getAge();
max=person.getMaximumHeartRate(age);
cout<<person.getfirstName()<<""<<person.getlastName()<<": ";
cout<<"Gender:"<<person.getGender()<<" age:"<<age<<endl;
h=person.getHeight();
cout<<"Height: "<<h<<" inches ";
w=person.getWeight();
cout<<"Weight: "<<w<<" pounds ";
cout<<"Maximum heart rate: "<<max<<endl;
person.getTargetHeartRate(max,a,b);
cout<<"Target heart rate range between: "<<a<<"and "<<b<<endl;
cout<<"BMI= "<<(w*703.)/pow(h,2)<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.