/* ********************************************
		Program: lab09_Ctp.cpp
		
		Tonya M. Payne
		COSC 1435 :: Section 3
		
		Version: 1.0
		Date:		Original 24 March 2010
		Purpose:	Write a program that uses functions to convert from meters
					to feet and back to meters
		
		Input: Keyboard
		Output: To screen


*///////////////////////////////////////////////

#include <iostream> //Used for cout
#include <iomanip>	//For output formatting
#include <cmath>  	//For math functions
#include <cstdlib>	//For other stuff

using namespace std; //compiler environment

//function prototypes
void printInfoStdToMetric( int, int, double );
double convertStdToMetric ( int , int ); 
void getStdInput( int &, int & ); 

void printInfoMetricToStd( double, int, double );
void convertMetricToStd ( double, int &, double & ); 
void getMetricInput( double & ); 



//global constants
const double	FEET_METERS = 3.28084;
const double	METERS_INCHES = 39.3700787;


//main part of program
int main()
{
	int 			iFeet,
					iInches,
					iSelection;					
	double			dInches,
					dMeters;
	
	
	do
	{
		cout << "Select \"1\" to convert from Standard to Metric or \n";
		cout << "select \"2\" to convert from Metric to Standard or \n";
		cout << "select \"3\" to exit the program: ";
		cin >> iSelection;	
		
			while ( iSelection < 1 || iSelection > 3 )
			{
				cout << "That is not a valid selection, please select 1 or 2: ";
				cin >> iSelection;
			}			
			
			if ( iSelection == 1 )
			{
				getStdInput( iFeet, iInches );
			
				dMeters = convertStdToMetric ( iFeet, iInches );
			
				printInfoStdToMetric( iFeet, iInches, dMeters );
			}
			else if ( iSelection == 2 )
			{
				getMetricInput( dMeters );
			
				convertMetricToStd ( dMeters, iFeet, dInches );
			
				printInfoMetricToStd( dMeters, iFeet, dInches );
			}
			
	} while ( iSelection != 3 );
	
	return 0;
}

/********************************************************************
*********************************************************************
//	function name: printInfoStdToMetric
//
//	purpose: 	This function prints the feet, inches, and meters passed
//				to it from the calling function.
//
//	parameters:		iFeetIn:	The number of feet.
//					iInchesIn:	The number of inches.
//					dMetersIn:	The number of meters.
//					
//	return type:	void
//
*********************************************************************
********************************************************************/

void printInfoStdToMetric( int iFeetIn, int iInchesIn, double dMetersIn )
{
	cout << "\nIn Standard Measurement, " << iFeetIn << " feet and " ;
	cout << iInchesIn << " inches" << " convert to ";
	cout  << dMetersIn << " \nmeters in Metric Measurement.\n" << endl;
}

/********************************************************************
*********************************************************************
//	function name: convertStdToMetric
//
//	purpose: 	To convert feet and inches to meters
//				
//
//	parameters:		dMetersOut:	the number of meters outputed to main
//										
//	return type:	double, in meters
//
*********************************************************************
********************************************************************/

double convertStdToMetric ( int iFeetIn, int iInchesIn )
{
	double dMetersOut;
	dMetersOut = iFeetIn / FEET_METERS;
	dMetersOut += ( iInchesIn / 12. ) * ( 1./FEET_METERS );
	return dMetersOut;
}

/********************************************************************
*********************************************************************
//	function name: getStdInput
//
//	purpose: 	To take the amount of feet and inches from the user
//				for the purpose of conversion and output to meters
//
//	parameters:		iFeetOut:	the number of feet
//					iInchesOut:	the number of inches
//					
//					
//	return type:	void
//
*********************************************************************
********************************************************************/

void getStdInput( int & iFeetOut , int & iInchesOut )
{
	cout << "Number of feet: ";
	cin >> iFeetOut;
	while ( iFeetOut < 0 )
	{
		cout << "Error: Feet cannot be negative!\n";
		cout << "Number of feet: ";
		cin >> iFeetOut;
	}
	
	cout << "Number of inches: ";
	cin >> iInchesOut;
	while ( iInchesOut < 0 )
	{
		cout << "Error: Inches cannot be negative!\n";
		cout << "Number of inches: ";
		cin >> iInchesOut;
	}
	if ( iInchesOut > 12 )
	{
		int iTooBig = iInchesOut / 12;
		iFeetOut += iTooBig;
		iInchesOut = iInchesOut % 12;
	}
}

/********************************************************************
*********************************************************************
//	function name: printInfoMetricToStd
//
//	purpose: 	This function prints the feet, inches, and meters passed
//				to it from the calling function.
//
//	parameters:		iFeetIn:	The number of feet.
//					iInchesIn:	The number of inches.
//					dMetersIn:	The number of meters.
//					
//	return type:	void
//
*********************************************************************
********************************************************************/

void printInfoMetricToStd( double dMetersIn, int iFeetIn, double dInchesIn )
{	
	cout << "\nIn Metric Measurement, " << dMetersIn << " meters " ;
	cout << "converts to " << iFeetIn << " feet and " ;
	cout  << dInchesIn << " \ninches in Standard Measurement.\n" << endl;
}

/********************************************************************
*********************************************************************
//	function name: convertMetricToStd
//
//	purpose: 	To convert meters to feet and inches
//				
//
//	parameters:		dInchesOut: the number of inches output
//					iFeetOut: the number of feet output
//					dMetersIn: amount of meters taken in
//
//	return type:	void
//
*********************************************************************
********************************************************************/

void convertMetricToStd (double dMetersIn, int & iFeetOut, double & dInchesOut)
{
	dInchesOut = dMetersIn * METERS_INCHES;
	iFeetOut = static_cast<int> ( dInchesOut  / 12 );
	dInchesOut = dInchesOut - ( iFeetOut * 12 );
	if ( dInchesOut > 11.99999 )
	{
		iFeetOut ++;
		dInchesOut = 0;
	}
}

/********************************************************************
*********************************************************************
//	function name: getMetricInput
//
//	purpose: 	To get input from the user in meters
//				
//	parameters:	dMetersOut: meters taken from user
//									
//	return type:	void
//
*********************************************************************
********************************************************************/

void getMetricInput( double & dMetersOut )
{
	cout << "Number of meters: ";
	cin >> dMetersOut;
	while ( dMetersOut < 0 )
	{
		cout << "Error: Meters cannot be negative!\n";
		cout << "Number of meters: ";
		cin >> dMetersOut;
	}
}
