为什么我的程序不触及我创建的功能?

Why doesn't my program touch the functions I made?

本文关键字:创建 功能 我的 程序 为什么      更新时间:2024-04-28

我在运行以下代码时遇到问题,它运行但不显示任何

#include <iostream>
using namespace std;
class Calorie {
public:
string name;
double height;
double weight;
char gender;
string fname;
string fgroup;
int cal;
double recommendcal;
double temp;
double RecCal();
Calorie(string foodname, string foodgroup, int calories);
Calorie(string n, double h, double w, char g);
};
class Foodgroup : public Calorie {
int rec;
void Grains();
void Proteins();
void Vege();
void fruit();
void Dairy();
};

int main() {
string foodname;
string foodgroup;
int calories;
string name;
double height;
double weight;
char gender;

cout << "Please enter your name:";
cin >> name;
cout << "Please enter your height in inches:";
cin >> height;
cout << "Please enter your weight in pounds:";
cin >> weight;
cout << "Please enter your gender F/M:";
cin >> gender;
Calorie human(name, height, weight, gender);
Calorie RecCal();
cout << "Enter Food name:";
cin >> foodname;
cout << "Enter Food type:";
cin >> foodgroup;
cout << "Enter number of calories:";
cin >> calories;
Calorie food(foodname, foodgroup, calories);
if (foodgroup == "grains") {
Foodgroup Grains();
}
else if (foodgroup == "proteins" || foodgroup == "Proteins") {
Foodgroup Proteins();
}
else if (foodgroup == "fruits" || foodgroup == "Fruits") {
Foodgroup fruits();
}
else if (foodgroup == "vegetables" || foodgroup == "Vegetables") {
Foodgroup Vege();
}
else if (foodgroup == "dairy" || foodgroup == "Dairy") {
Foodgroup Dairy();
}
return 0;
}

Calorie::Calorie(string n, double h, double w, char g) {
name = n;
height = h;
weight = w;
gender = g;

}
Calorie::Calorie(string foodname, string foodgroup, int calories) {
fname = foodname;
fgroup = foodgroup;
cal = calories;
}
double Calorie::RecCal() {
if (gender =='M' || gender == 'm') {
recommendcal = 2500;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 10 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 10 * temp;
recommendcal = recommendcal + temp;
}
}
else if (gender == 'F' || gender == 'f') {
recommendcal = 2300;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}
else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 5 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 5 * temp;
recommendcal = recommendcal + temp;
}
}
cout << "You're recommended calorie intake per day is: " << recommendcal << " calories." << endl;
return recommendcal;

}

void Foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Proteins() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Vege() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::fruit() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::Dairy() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}

我不确定为什么会发生这种情况,如果你能帮助我解决这个问题,我将不胜感激。我不知道如何解决这个问题,看起来一切都应该奏效,但有些事情没有成功。这些代码的目的是计算摄入的卡路里量,看看它们是否达到了建议的卡路里摄入量。它还应该问三次,三种不同的食物/饮料,因为我们一天不仅仅吃/喝一件事。

替换:

void Human(Calorie name, Calorie height, Calorie weight, Calorie gender)

带有:

void Human(std::string name, double height, double weight, char gender)

函数签名中的类型错误。

Ron,没错,它解决了这个问题,但还有另一个问题:

void foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}

[错误]未在此范围中声明"recommocal">

没有"推荐"的声明