Skip to content

Instantly share code, notes, and snippets.

@austenstrine
Last active February 6, 2017 17:55
Show Gist options
  • Save austenstrine/cfe89a7495ed44008bfd8a20bb7bc710 to your computer and use it in GitHub Desktop.
Save austenstrine/cfe89a7495ed44008bfd8a20bb7bc710 to your computer and use it in GitHub Desktop.
//I got a tiny bit further in the book and realized I was
//coding pretty inefficiently in the switch/case area.
//These are the updates I made to that.
//
#include <iostream>
#include <iomanip>
#include <typeinfo>
#include <limits>
using namespace std;
int main()
{
char gender = ' ';
char activity = ' ';
int weight = 0;
double caloriesPP = 0.0;
do
{
cout << endl << "Enter Gender (m/f):" << endl;
cin >> gender;
gender = toupper(gender);
if (gender != 'M' && gender != 'F')
{
cout << endl << "Invalid input. Please use only M/F." << endl;
} //end if
} while (gender != 'M' && gender != 'F');//end do/while
do
{
cout << endl << "Enter Activity level (a/i):" << endl;
cin >> activity;
activity = toupper(activity);
if (activity != 'A' && activity != 'I')
{
cout << endl << "Invalid input. Please use only A/I." << endl;
} //end if
} while (activity != 'A' && activity != 'I');//end do/while
do
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//I had to look ^this^ up to get it to stop freaking out when I would put in a
//string/char instead of a number. Sounds like it basically empties whatever is
//in the >> for an unlimited number of characters until it reaches a linebreak.
cout << endl << "Enter weight (lbs):" << endl;
cin >> weight;
if (weight <= 0)
{
cout << endl << "Invalid input. Please enter an integer greater than 0." << endl;
}
} while (weight <= 0);
//I wasn't sure if it was bad form or not to use default as a result rather than
//an error, but I figured I should learn both ways. Is more efficiency > easier
//debugging? It depends on the team, I'm guessing.
switch (gender)
{
case 'M':
switch (activity)
{
case 'A':
caloriesPP = weight * 15;
break;
default:
caloriesPP = weight * 13;
break;
}//end M/AI switch
break;
default:
switch (activity)
{
case 'A':
caloriesPP = weight * 12;
break;
default:
caloriesPP = weight * 10;
break;
}//end F/AI switch
break;
}//end switch
cout << endl << "Calories needed to maintain weight:" << endl << caloriesPP;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment