Constants and switch statement? - C++ beginner programming
May 4, 2017 at 4:07am UTC
Hi.
I'm doing an assignment for school and it says that I must use constants for the menu choices. And I must use constants for Mets values and I also must use a switch statement. I don't know what any of that means.. I would ask my professor but I don't have class until next week.. The program runs perfectly and does what it is supposed to do, which is to work like a calorie calculator. However, I have to follow the exact instructions. Anyone who could try to explain to me what I have to do to follow those instructions? Here is my code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float weightInPounds, mets, mins, totalCal, calPerMinute;
cout << "Hello and Welcome to the Calorie Calculator!\n" ;
// Prompting the user to enter weight
cout << "Please Enter Your Weight in Pounds: " << endl;
cin >> weightInPounds;
// Prompting the user to enter number of mets
cout << "Please select your exercise:" << endl;
cout << "1. Running" << endl;
cout << "2. Basketball" << endl;
cout << "3. Sleeping" << endl;
cout << "4. Quit" << endl;
int choice;
cin >> choice;
if (choice == 1)
mets = 10;
else if (choice == 2)
mets = 8;
else if (choice == 3)
mets = 1;
else if (choice == 4)
cout << "See you next time!" << endl;
else
cout << "Invalid selection" << endl;
// Prompting the user to enter the duration
cout << "Enter the Duration in Minutes: " << endl;
cin >> mins;
// Adding two decimals to the results
cout << fixed << setprecision (2);
// Calculating the total amount of calories burned
calPerMinute = 0.0175 * (weightInPounds / 2.2) * mets;
totalCal = calPerMinute * mins;
// Displaying the results
cout << "You burned an estimated " << totalCal << " calories." << endl;
return 0;
May 4, 2017 at 4:54am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <iostream>
#include <iomanip>
int main()
{
// use constants for Mets values
const int Mets_Running = 10 ;
const int Mets_Basketball = 8 ;
const int Mets_Sleeping = 1 ;
std::cout << "Hello and Welcome to the Calorie Calculator!\n\n" ;
// Prompting the user to enter weight
std::cout << "Please Enter Your Weight in Pounds: " ;
double weightInPounds = 0 ;
std::cin >> weightInPounds;
if ( weightInPounds <= 0 )
{
std::cout << "invalid weight\n" ;
return 1 ; // non-zero to indicate failure
}
std::cout << "Please select your exercise:\n"
<< "1. Running\n"
<< "2. Basketball\n"
<< "3. Sleeping\n"
<< "4. Quit\n\n"
<< "? " ;
int choice = 0 ;
std::cin >> choice ;
double mets = 0 ;
// use a switch statement (replace the if - else if construct)
// see switch statement (bottom of the page) in
// http://www.cplusplus.com/doc/tutorial/control/
switch (choice)
{
case 1: mets = Mets_Running ; break ; // 1. Running
case 2: mets = Mets_Basketball ; break ; // 2. Basketball
case 3: mets = Mets_Sleeping ; break ; // 3. Sleeping
case 4: std::cout << "See you next time!\n" ; break ; // 4. Quit
default : std::cout << "Invalid selection\n" ; return 1 ;
}
if ( mets > 0 ) // if running, basketball or sleeping
{
// Prompting the user to enter the duration
std::cout << "Enter the Duration in Minutes: " ;
int mins = 0 ;
std::cin >> mins;
if ( mins > 0 ) // valid non-zero duration
{
const double calPerMinute = 0.0175 * ( weightInPounds / 2.2 ) * mets ;
const double totalCal = calPerMinute * mins;
std::cout << "\nYou burned an estimated " << std::fixed
<< std::setprecision (2) << totalCal << " calories.\n" ;
}
}
// there is an implicit return 0 ; at the end of main
}
Topic archived. No new replies allowed.