为什么我的程序不触及我创建的功能?
Why doesn't my program touch the functions I made?
我在运行以下代码时遇到问题,它运行但不显示任何
#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">
没有"推荐"的声明
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- C++ - 为文本RPG创建基本战斗功能
- 我在 C++ 中创建了一个函数来递归反转字符串,但是之后如何使功能打印一个 endl?
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 创建类成员功能
- 插入功能保持重新创建根节点
- 如何在 python 交易模型中创建功能时间条件
- 如何为带有计时功能的程序创建计时器?
- Protobuf的反序列化功能之一可以使用Cereal重新创建吗?
- 如何使用Sfinae为容器创建模板功能,并根据操作员推断返回类型
- 类自动创建基于成员名称的功能
- 在功能中创建一个数组,然后将其返回到main()
- 从没有预处理器的HANA元组中创建功能签名
- 如何创建功能以用受保护或私人侵犯的对象在对象内部操纵对象
- 在堆中创建功能指针的数组
- 是否有一种方法可以避免在RVALUE和LVALUE参考中创建功能时避免重复的代码
- WXWIDGETS WXPANEL访问创建功能中创建的WXPanelNamest
- 在C 中的预局体中创建功能
- 在Windows中创建功能齐全的MP3播放器
- 在c++中使用字符串,分析字符并通过类创建功能