程序正在执行,但没有打印正确的详细信息



我的任务是创建一个程序,该程序记录用户的姓名/出生日期,并打印回人名/出生日期、年龄和目标心率。

它让我抓狂,因为我可以让程序执行,但它给了我疯狂的答案,我不知道为什么。

如有任何建议,我们将不胜感激。请找到我的main.cpp/HeartRates.cppHeartRates.h的代码(还附上了我当前输出的屏幕截图(。

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

删除setters中带有this的类型。

例如:

void HeartRates::setDayOfBirth(int day) {
int dayOfBirth = day;
}//end day of birth function

int dayOfBirth = day;生成一个名为dayOfBirth的新变量,而不更新类的字段。要更新类的字段,不需要编写变量的类型。

比如:(可以添加this(

void HeartRates::setDayOfBirth(int day) {
this->dayOfBirth = day;
}//end day of birth function

访问成员变量

您似乎混淆了变量声明和为成员变量赋值。

void HeartRates::setMonthOfBirth(int month) {
int monthOfBirth = month;
}

在上面的例子中,您声明了一个名为monthOfBirth的局部变量,将变量month的值分配给它,在方法作用域的末尾,它被销毁,而不会发生任何其他事情。您需要以下内容:

void HeartRates::setMonthOfBirth(int month) {
monthOfBirth = month;
// or
this->monthOfBirth = month;
}

构建您的对象

您的构造函数根本没有初始化成员变量。应该是这样的:

HeartRates::HeartRates(): firstName("unknown"), lastName("unknown"), dayOfBirth(0), monthOfBirth(0), yearOfBirth(0)
{}

相关内容

  • 没有找到相关文章

最新更新