I have been tasked with creating a program that takes a users name / date of birth, and prints back the persons name / date of birth, age and their target heart rate.
It is driving me crazy as I can get the program to execute, but it gives me crazy answers, and I've no indication as to why.
Any advise would be greatly appreciated. Please find the code for my main.cpp
/ HeartRates.cpp
and HeartRates.h
(also attached is a screenshot of my current output).
main.cpp
:
#include <iostream>
#include "HeartRates.h"
using namespace std;
int main() {
int d, m, y;
string firstName, lastName;
HeartRates heart;
cout << "Please enter your first name: ";
cin >> firstName;
heart.setFirstName(firstName);
cout << "Please enter your last name: ";
cin >> lastName;
heart.setLastName(lastName);
cout << "What year were you born: ";
cin >> y;
heart.setYearOfBirth(y);
cout << "What month were you born on: ";
cin >> m;
heart.setMonthOfBirth(m);
cout << "What day were you born on: ";
cin >> d;
heart.setDayOfBirth(d);
int age = heart.getAge();
int maxHR = heart.getMaxHeartRate(age);
heart.displayHeartRates(age);
heart.getTargetHeartRate(maxHR);
}
HeartRates.h
:
#include <string>
class HeartRates {
public:
explicit HeartRates();
HeartRates(std::string firstname, std::string lastname, int day, int month, int year);
void setFirstName(std::string); //set 1st name
void setLastName(std::string); //set 2nd name
void setDayOfBirth(int); //set 'day' of birth
void setMonthOfBirth(int); //set 'month' of birth
void setYearOfBirth(int); //set 'year' of birth
std::string getFirstName() const; //get 1st name
std::string getLastName() const; //get last name
int getDayOfBirth() const; //get Day of Birth
int getMonthOfBirth() const; //get Month of Birth
int getYearOfBirth() const; //get Year of Birth
int getAge(); //Function to get and return age
int getMaxHeartRate(int); //Function to get max heart rate
void getTargetHeartRate(int); //Function to get target heart rate
void displayHeartRates(int); //Function to display heart rate
public:
std::string firstName;
std::string lastName;
int dayOfBirth;
int monthOfBirth;
int yearOfBirth;
};// terminate
HeartRates.cpp
:
#include <iostream>
#include "HeartRates.h"
using namespace std;
// default constructor
HeartRates::HeartRates() {
std::string firstName = "unknown";
std::string lastName = "unknown";
int dayOfBirth = 0;
int monthOfBirth = 0;
int yearOfBirth = 0;
}// end default constructor
//2nd constructor
HeartRates::HeartRates(std::string firstname, std::string lastname, int day, int month, int year) {
string setFirstName(firstname);
string setLastName(lastname);
int setDayOfBirth(day);
int setMonthOfBirth(month);
int setYearOfBirth(year);
}// end 2nd constructor
//1st name function
void HeartRates::setFirstName(string firstname) {
string firstName = firstname;
}//end 1st name function
//last name function
void HeartRates::setLastName(string lastname) {
string lastName = lastname;
}//end last name function
// set day of birth
void HeartRates::setDayOfBirth(int day) {
int dayOfBirth = day;
}//end day of birth function
//set month of birth
void HeartRates::setMonthOfBirth(int month) {
int monthOfBirth = month;
}//end month of birth function
//set year of birth
void HeartRates::setYearOfBirth(int year) {
int yearOfBirth = year;
}//end year of birth
// get 1st name function
string HeartRates::getFirstName() const {
return firstName;
}// end 1st name function
// get last name function
string HeartRates::getLastName() const {
return lastName;
}// end last name function
// get day of birth function
int HeartRates::getDayOfBirth() const {
return dayOfBirth;
}//end day of birth function
// get month of birth function
int HeartRates::getMonthOfBirth() const {
return monthOfBirth;
}//end month of birth function
// get year of birth function
int HeartRates::getYearOfBirth() const {
return yearOfBirth;
}//end year of birth function
//Calculate age function
int HeartRates::getAge() {
int age;
int d;
int m;
int y;
cout << "enter current year: \n" << endl;
cin >> y;
cout << "enter current month: \n" << endl;
cin >> m;
cout << "Enter current day: \n" << endl;
cin >> d;
if (getMonthOfBirth() < m) {
m = m - getMonthOfBirth();
} else {
m = getMonthOfBirth() - m;
}
if (getDayOfBirth() < d) {
d = d - getDayOfBirth();
} else {
d = getDayOfBirth() - d;
}
age = y - getYearOfBirth();
return age;
} //end calculate age function
//calculate max heart rate function
int HeartRates::getMaxHeartRate(int age) {
//220 - age
int maxHR = 220 - age;
return maxHR;
}//end max heart rate function
//calculate target heart rate function
void HeartRates::getTargetHeartRate(int maxHR) {
// target heart rate is between 50% & 85% of maximum heart rate
cout << "Your target heart rate is between " << maxHR * 0.5 << " and " << maxHR * 0.85 << "bpm. ";
}//end of target heart rate function
// display info function
void HeartRates::displayHeartRates(int age)
{
int a = age;
cout << "Hello " << getFirstName() << " " << getLastName() << endl;
cout << "Your DOB is " << getDayOfBirth() << "/" << getMonthOfBirth() << "/" << getYearOfBirth() <<
endl;
cout << "Your age is " << a << "years old" << endl;
} // end display info function
Remove the type with
this
in the setters.for example:
int dayOfBirth = day;
make a new variable with the name of dayOfBirth and not update the field of the class. To update the field of the class you don't need to write the type of the variable.like that: (It is fine to add
this
)