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;
}

>> don't know how to separate it like this.
see frexp() in math.h

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;
}

The extraction operator ">>" for cin is overloaded to allow for a different extraction types. In otherwords you can extract an int and double data type with one cin statement, such as the following:

int a;
double b;
cout << "Enter and integer and a double (i.e. 130 5.22)" << endl;
cin >> a >> b;
cout << "Integer = " << a << " Double = " << b << endl;

As for printing out a double number so that you can print the meters and the left over centimeters, you can try to cast the double to an int therefore extracting the meters, then subtract that number from the double and you've got the centimeters portion. For the actual conversions, it might be helpful to referrence the conversion factors http://www.infoplease.com/ipa/A0001729.html.

Good luck, LamaBot

The parsing of the input is going to be the hardest thing. Initially, make the input simple. Accept 2 numbers, feet/inches or meters/centimeters.

Then combine ft/in into inches, m/cm into centimeters. Then convert to the other system and split. Switching everything to the lowest value (in/cm) makes the use of doubles easy, no splitting is really necessary (2431.23cm = 24M, 31.23cm). Simple...

I think I am making this harder than it needs to be. I have a general idea but something I'm doing is off. In the feet2Meters function I am using 39 instead of 39.37 because I don't know how to make that work with int feet and double inches. I've made everything doubles so it's easier. I did the same in the meters2Feet function. I used 30 instead of 30.48. I think I've gone as far as I know how. It has been a day of frustration! This is my most recent attempt:

/* --------------------------------------------
 Converter.cpp
 Rachel Pope
 COP2334
 2/27/07
 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, double& centimeters);
void feet2Meters(double& feet, double& inches, double& meters, double& centimeters);
void feet2MetersOutput(double& meters, double& centimeters);
void meters2FeetInput(double& meters, double& centimeters, double& feet, double& inches);
void meters2Feet(double& meters, double& centimeters, double& feet, double& inches);
void meters2FeetOutput(double& feet, double& inches);
 
 

void main()
{
 double f, i, m, c;
 int choice;
 do{
  choice = getChoice();
  if( choice == 1 ) feet2MetersInput(f, i, m, c);
  else if( choice == 2 ) meters2FeetInput(m, c, f, i);
  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, double& centimeters)
{
 cout << "Enter feet.\n";
 cin >> feet;
 cout << "Enter inches.\n";
 cin >> inches;
 feet2Meters(feet, inches, meters, centimeters);
 
 
}
//This function converts feet and inches into meters and centimeters.
void feet2Meters(double& feet, double& inches, double& meters, double&centimeters)
{
 double answer1 = 0;
 answer1 = (feet*12);
 inches = (answer1 + inches);
 meters = static_cast<int>(inches)/(39);
 centimeters = static_cast<int>(inches)%(39);
 feet2MetersOutput(meters, centimeters);
}
 
//This function outputs the conversion.
void feet2MetersOutput(double& meters, double& centimeters)
{
 cout << "There are " << meters << " meters and " << centimeters << " centimeters." << endl;
}
 
//This function allows the user to enter their measurements for conversion.
void meters2FeetInput(double& meters, double& centimeters, double& feet, double& inches)
{
 cout << "Enter meters.\n";
 cin >> meters;
 cout << "Enter centimeters.\n";
 cin >> centimeters;
 meters2Feet(meters, centimeters, feet, inches);
 
}

//This function converts meters and centimeters into feet and inches.
void meters2Feet(double& meters, double& centimeters, double& feet, double& inches)
{
 double answer2 = 0;
 answer2 = meters*100;
 centimeters = (answer2 + centimeters);
 feet = static_cast<int>(centimeters)/(30);
 inches = static_cast<int>(centimeters)%(30);
 meters2FeetOutput(feet, inches);
}

//This function outputs the conversion.
void meters2FeetOutput(double& feet, double& inches)
{
 cout << "There are " << feet << " feet and " << inches << " inches." << endl;
}

I think I am making this harder than it needs to be.

Yes you are. Did you read what I posted? It would generate fairly short code. And you don't have to (and probably shouldn't in general) call a calculate function from the input function. Just work on one side of the problem first (say English to metric) then when that's running, add the other conversion.

And indent more that 1 space, please. 3-4 is standard.

And main() is not a void, it's an int

I understand and believe that I have done what you said about converting by the smallest units first. My main problem is just this: " (2431.23cm = 24M, 31.23cm)."

Like my ex:

double answer1 = 0;
    answer1 = (feet*12);
    inches = (answer1 + inches);
    meters = inches/39.37;
    cout << "There are this many " << meters << " meters and centimeters." << endl;

I first multiplied the feet by inches to get the total number of inches from the feet input. I then added the number of inches entered to reach the total number of inches. I then divided inches by the number of inches in a meter to get the number of meters. And then I divided the inches by this again using the % sign to get the remaining # of centimeters. But according to converters I've used online my answers are always a little off. So I really can't see the problem myself. It seems like I've got it down but something actually is wrong.

Yes you are. Did you read what I posted? It would generate fairly short code. And you don't have to (and probably shouldn't in general) call a calculate function from the input function. Just work on one side of the problem first (say English to metric) then when that's running, add the other conversion.

And indent more that 1 space, please. 3-4 is standard.

And main() is not a void, it's an int

Programming can be daunting sometimes but it helps to reflect and have a glass of water. Waltp has a good suggestion - you can just use doubles variables and it is relatively easier to split them. Becuase of how simple the functions might be, I'd just make them inline.

Good luck and hang in there, LamaBot

I understand and believe that I have done what you said about converting by the smallest units first. My main problem is just this: " (2431.23cm = 24M, 31.23cm)."

Like my ex:

double answer1 = 0;
    answer1 = (feet*12);
    inches = (answer1 + inches);
    meters = inches/39.37;
    cout << "There are this many " << meters << " meters and centimeters." << endl;

I first multiplied the feet by inches to get the total number of inches from the feet input. I then added the number of inches entered to reach the total number of inches. I then divided inches by the number of inches in a meter to get the number of meters. And then I divided the inches by this again using the % sign to get the remaining # of centimeters. But according to converters I've used online my answers are always a little off. So I really can't see the problem myself. It seems like I've got it down but something actually is wrong.

Maybe you can start one-by-one. Write a function to convert inches to feet. Create another function to convert feet to meters. Then to convert inches to meters, use the inches-to-feet and feet-to-meters function.

Example:

double inch2feet(double inches) {
      return inches / 12;
}
double feet2meter(double feet) {
    return feet * 3.2808;
}
double inches2meter(double inches) {
   return feet2meter(inch2feet(inches));
}
 
// One way to split the meters
double meter=24.42;
int significant = (int)meter;
cout << "There are " << significant << " meters and " << meter - (double)significant << " centimeters";

Good luck, LamaBot

I understand and believe that I have done what you said about converting by the smallest units first. My main problem is just this: " (2431.23cm = 24M, 31.23cm)."

Like my ex:

double answer1 = 0;
    answer1 = (feet*12);
    inches = (answer1 + inches);
    meters = inches/39.37;
    cout << "There are this many " << meters << " meters and centimeters." << endl;

I first multiplied the feet by inches to get the total number of inches from the feet input. I then added the number of inches entered to reach the total number of inches. I then divided inches by the number of inches in a meter to get the number of meters. And then I divided the inches by this again using the % sign to get the remaining # of centimeters. But according to converters I've used online my answers are always a little off. So I really can't see the problem myself. It seems like I've got it down but something actually is wrong.

Which is larger, meters or centimeters? My suggestion was convert feet/inches to inches (smallest) then convert to cm (smallest). Then it's a simple matter to get the meters. It's less exact to go the way you did.

And because your code is so huge, I'm suggesting starting over and thinking about how to make the program piece by small piece rather than all at once like you have now. A lot of what you have can simply be copied into the new code (intelligently) so it's not a total rewrite.

To illustrate:

Step 1 from main():

Call English input function:
    Accept feet/inches.
    Display input to verify it worked.
    Convert to inches
    Display inches to verify it worked.
    return inches

In main(), display inches to verify it worked.

Step 2 from main():

After call to English input function 
Call English to Metric conversion function, pass in inches
    convert inches to cm
    return cm

In main(), display cm to verify it worked.

etc...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.