//
//
//  weight.cc
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Two, Main - question 7 [worth 3pts],  7/3/2002
//
//
// Description: asks for persons sex and height, then prints their ideal weight
//
//              A man should weigh 106 pounds for the first 5 feet of height, 
//               plus 7 pounds for every inch above that
//
//              A woman should weigh 100 pounds for the first 5 feet of height,
//               plus 6 pounds for every inch above that
//
//       Usage: When prompted, key in sex (1 for female, 2 for male),
//               then key in height in feet and inches
//
//      Method: main calls three primary functions
//              handleSex() - asks user if they are male or female and sets vars
//              howTall()   - asks how tall user is and prints ideal weight
//                ...for all user input a special function s used
//              getInt()    - reads an integer from keyboard, checking for errors
//
//       Notes: Properties for this program are stored as globals
//              as we haven't covered classes/properties/methods yet!


#include <iostream>
#include <iomanip>

// constants
const int MALE5FOOTWEIGHT   = 106;
const int FEMALE5FOOTWEIGHT = 100;

// globals
// ...should be properties in a class, but we haven't covered classes yet!
//
int    poundsPerInch,     // pounds of weight added for each inch of height
	   baseWeight;        // base weight added for 1) female or 2) male
       

//
//  reads an integer from the keyboard,
//  uses error checking to ensure value is between minValue and maxValue
//
int getInt(char askStr[], int minValue, int maxValue)
{
	int mVar;
	
	while (1) { // ...while TRUE
	
		// ask query
		cout << askStr;
		cin >> mVar;
		
		if ( mVar >= minValue && mVar <= maxValue )
			break;  // input OK, break from loop
		else {
			cout << "Bad input: please enter a value between "
			     << minValue << " and " << maxValue << endl;
			cin.clear();
			cin.ignore(80, '\n');
		}
	}
	
	return mVar;
}


//
//  Ask user their sex and set properties
// 
void handleSex()
{
	if (getInt("Type 1 if you are female and 2 if male: ", 1, 2) == 1) {
	
	    // ...is a female
		baseWeight    = FEMALE5FOOTWEIGHT;
		poundsPerInch = 6;
	}
	else {
	            
		// ...is a male
		baseWeight    = MALE5FOOTWEIGHT;
		poundsPerInch = 7;
	}
}


//
//  Ask how tall user is
//    and print their appropriate weight
// 
void howTall()
{
    int feet,             // height of user (feet)
        inches,           // height of user (inches)
        weight;           // estimated weight
        
	cout << "How tall are you? \n";

    // min height is 5 foot, max height is 9'11
	feet   = getInt("  Number of feet: ", 5, 9);
	inches = getInt("  Number of inches: ", 0, 11);
	
	// calc their weight
	weight = (((feet - 5) * 12) + inches) * poundsPerInch + baseWeight;
	
	// print
	cout << "You should weigh " << weight << " pounds" << endl;
}


//
//  Main calls the two primary functions
//
void main()
{
	handleSex();
	howTall();
}