I am writing a C++ program to convert feet and inches to meters and centimeters and vice versa. I am having a problem figuring out the conversions. What I have so far actually works...it gives me a result but my way of getting to that result is not the correct one. They way I have it, the output is like this ex: 1.123 (this would be the meters and cm.) I need it to say maybe ex: " 1 meter and .123 centimeters." I don't know how to separate it like this. It is also o.k. for the user to enter 6 feet and 37 inches. The program should be able to use this. I can't figure out how to do this though because I will actually need feet and meters to be ints and inches and centimeters to be doubles. Right now I have them all set as doubles. I seem to always get stuck on the mindless math conversions. I can do the main bulk of the program myself, but search all over for mathematical equations/ conversions. I don't want that to affect my grade...I feel like I'm watsing my time.
I thought about using, for ex: 17/2 to get the feet and then 17%2 to get the inches, but I can't use both feet and inches.
I am also told that there are .3078 meters/foot and 100 cm/meter.
I hope I have made my problem clear enough. The code I have below works just fine. I have no problems with the functions or parameters. Any help will be greatly appreciated! Thank you! :cheesy:
/* --------------------------------------------
Conversions.cpp
This is a program that allows the user to select their choice of conversion from a menu.
The program uses functions to perform these conversions and supply the user with a
result. The menu is then re-displayed after each conversion is performed.
--------------------------------------------*/
#include <iostream>
#include <cmath>
using namespace std;
int getChoice();
void feet2MetersInput(double& feet, double& inches, double& meters);
void feet2Meters (double& feet, double& inches, double& meters);
void meters2FeetInput(double& meters, double& centimeters, double& feet);
void meters2Feet(double& meters, double& centimeters, double& feet);
void main()
{
double f, i, m, c;
int choice;
do{
choice = getChoice();
if( choice == 1 ) feet2MetersInput(f, i, m);
else if( choice == 2 ) meters2FeetInput(m, c, f);
if( choice > 2 || choice < 1 ) cout << "ERROR: Please enter valid choice (1 or 2)." << endl;
} while( choice != 3 );
}
//This function displays the calculation menu.
int getChoice()
{
int c;
cout << "----------------------\n";
cout << "1 - Feet and inches into meters and centimeters\n";
cout << "2 - Meters and centimeters into feet and inches\n";
cout << "3 - Quit Program\n";
cout << "----------------------\n";
cout << "Enter number of choice --> ";
cin >> c;
return c;
}
//This function allows the user to input their measurements for conversion.
void feet2MetersInput(double& feet, double& inches, double& meters)
{
cout << "Enter feet.\n";
cin >> feet;
cout << "Enter inches.\n";
cin >> inches;
feet2Meters(feet, inches, meters);
}
//This function converts feet and inches into meters and centimeters.
void feet2Meters(double& feet, double& inches, double& meters)
{
double answer1 = 0;
answer1 = (feet*12);
inches = (answer1 + inches);
meters = inches/39.37;
cout << "There are this many " << meters << " meters and centimeters." << endl;
}
//This function allows the user to enter their measurements for conversion.
void meters2FeetInput(double& meters, double& centimeters, double& feet)
{
cout << "Enter meters.\n";
cin >> meters;
cout << "Enter centimeters.\n";
cin >> centimeters;
meters2Feet(meters, centimeters, feet);
}
//This function converts meters and centimeters into feet and inches.
void meters2Feet(double& meters, double& centimeters, double& feet)
{
double answer2 = 0;
answer2 = meters/100;
centimeters = (answer2 + centimeters);
feet = centimeters/30.48;
cout << "There are this many " << feet << " feet and inches." << endl;
}