c++ - CS1项目帮助C++
问题描述
我正在尝试这样做,以便当用户在第一个 cin 中输入数字 1-5 时,它将 int 锻炼分配给所选的锻炼名称。基本上,该作业使用锻炼、时间和人的体重来计算他们会从锻炼中燃烧多少卡路里。我将所有变量都定义为整数,对吗?
#include <iostream>
using namespace std;
int main()
{
//menu structure
cout << " ________________________________\n";
cout << "| Welcome to My Fitness Center |\n";
cout << "|--------------------------------|\n";
cout << "| MAIN MENU |\n";
cout << "| 1) Rowing Machine |\n";
cout << "| 2) Running |\n";
cout << "| 3) Weight lifting |\n";
cout << "| 4) Yoga |\n";
cout << "| 5) END |\n";
cout << "|________________________________|\n";
cout << "Enter the workout that you wish to track, or END to exit: \n";
//prompts user to choose which workout they are going to be doing
cin >> workout;
//**Right here is where I am having an issue. I am not sure how to make it so that when the user enters "1" into the console that it changes the value of 'workout' to Rowing Machine etc.**
cout << "Enter your weight in pounds: \n";
cin >> weight;
cout << "Enter the number of minutes: \n";
cin >> minutes;
weightKG = weight/2.2;
caloriesBurned = (minutes/60)*(met)*(weightKG)/2.2;
cout << "The total calories burned for " << workout << " was " << caloriesBurned << ".\n";
return 0;
}
注意:本项目不允许使用函数
解决方案
您不需要if
or switch
,您可以将值放入数组中:
double met[5] =
{
/* 0 */ 0.0, // This slot not used.
/* 1 */ 1.6, // the factor for using rowing maching.
/* 2 */ 0.8, // The factor for running,
// ...
};
double calories_burned = (minutes/60)*(met[selection])*(weightKG)/2.2;
您需要if
用于范围检查和退出程序。
推荐阅读
- arrays - 无法在不打印数组内容的情况下访问数组中的内存位置
- python - 如何使用python从网站获取主机名
- spring-boot - Spring Boot中如何发起一个所有控制器都可以访问的全局方法
- sql - Oracle SQL in 和 not in 都返回空
- bash - 如何在 bash 脚本输出中添加另一列?
- php - 使用 PHP 访问嵌套数组中的数据
- javascript - 如何生成单词数等于#的单词数组(例如value_1,value_2,...,value_#)?
- php - 每当用户登录时,Laravel 都会保留会话
- sql - 手机号码在 Oracle SQL 中应该从 3 开始,并且检查长度应该是 10
- python - 如何在公司代理后面构建 docker 容器?