首页 > 解决方案 > 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;
}

注意:本项目不允许使用函数

标签: c++

解决方案


您不需要ifor 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用于范围检查和退出程序。


推荐阅读