CS Project 2 - How much chocolate can you eat without gaining weight?

Program description:


The user is prompted for his/her age, height, weight, and gender.
The users info goes through an if/else statement ( Boolean expression ) and calculates his/here bmr based on his/her gender.
The user is then told how many chocolate bars he or she can eat without gaining any weight :-)

Any suggestions or comments for improvment are welcome!


#include <iostream>
using namespace std;

int main()
{

// "magic formula" to force any amount of decimal points for value type double

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);

// Variable Declarations: chocolate variable is already set to 230.

  double weight, height, bmr, total_bars, age, chocolate(230);
  char gender;

//  Program Intro - explaining to the user what the program will do.

   cout << "This program calculates how many choclate bars you can eat without gainging any weight.
";

// Inputs - prompting the user for all of his/her information required to enter into our formula

  cout << "Please enter your weight in pounds: 
";
  cin >> weight;
  cout << "Please enter your height in inches: 
";
  cin >> height;
  cout << "Please enter your age in years: 
";
  cin >> age;
  cout << "Male or Female? Enter m for male, f for female.
";
  cin >> gender;

// Processing - an if/else statement was required to seperate the two different possible gender formulas.
// Take note that without the ' ' around the m, the if/else statement will not work.

  if (gender == 'm')
  bmr = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
  else
  bmr  = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);

  total_bars = (bmr/chocolate);

// Output - giving the user his/her result. Maybe they should eat something healthier, like an apple.

  cout << "You can eat a total of 
" << total_bars << " chocolate bars without gaining any weight! :smile: 
";

return 0;

}

Just a minor thing maybe but one thing you could do
Maybe declare a switch statement, for the character input instead of If statement

 
switch(gender){
    case 'm':
    case 'M':
      bmr = 66 + (6.3 * weight) + (12.9 * height) - (6.8 * age);
    break ; 
    case 'f':
    case 'F':
      bmr  = 655 + (4.3 * weight) + (4.7 * height) - (4.7 * age);
    break;
    default:
       cout << "Invalid Gender" ; 
    break ; 
}

covers all possibilities then

I agree with Londoner1234 and the only other thing I’d do is eliminate the need for specifying a case for the gender by saying:


cin >> gender;
gender = tolower(gender);

But even that’s not necessary. It looks good!

Londoner1234 and Geozoide, I have not done switches yet so I’m not sure what those are. Right now in class we are covering two different types of loops ( while, while do ), and branching / nesting with a bunch of “if else” statements. A bunch of algorithm work and flow control stuff too/

Today I’m learning switches! Coincidence? I think not!

I keep clicking on this thread and I don’t understand anything that’s going on every time

Yeah, I was just going to describe what a switch statement was, but it seems you’ve already learned it!

1 Like