My goal is to write a problem that will read in a length in feet and inches and output the equivalent length in meters and centimeters. As noted, There are 0.3048 meters in a foot, 100 centimeters in a meter, and 12 inches in a foot. I am only having problems with the calculations. Hopefully someone can help me with it because I'm not good with math lol.

For example:

5 feet and 8 inches is equivalent to 1.7272 meters and 172.72 centimeters.

#include <iostream>
#include <stdio.h>

using namespace std;

const double meters_per_foot = 0.3048;
const int centimeters_per_meter = 100;
const int inches_per_foot = 12;

void getValues(double& feet, double& inches);
void calculate(double feet,double inches, double& meters, double& centimeters);
void print(double feet,double inches,double meters,double centimeters);

int main ()
{

double feet=0,inches=0;
double meters=0,centimeters=0;
char letter;

getValues(feet,inches);
calculate(feet,inches,meters,centimeters);
print(feet,inches,meters,centimeters);

system ("PAUSE");
return 0;

}

void getValues(double& feet, double& inches)
{
cout<<"Enter the number of feet: "<<endl;
cin>>feet;
cout<<"Enter the number inches: "<<endl;
cin>>inches;
}
void calculate(double feet,double inches, double& meters, double& centimeters)
{

centimeters = (inches_per_foot * 2.5400);
meters = (inches * meters_per_foot);

}
void print(double feet,double inches,double meters,double centimeters)
{
cout<<feet<<" feet and "<<inches<<" inches  is equivalent to: "<<meters<<" meters  and "<<centimeters<<" centimeters."<<endl;
}

>>centimeters = (inches_per_foot * 2.5400);
To convert feet to centimeters feet * 30.48

Answered by VernonDozier 2,218 in a post from

I just tried and did not get the result I displayed in my first post. I want to put in 5 for feet and 8 and inches to recieve 1.7272 in meters and 172.72 in centimeters.

Well, if you changed the code, please post the updated code.

Feet and inches should be integers, not doubles because they have no fractional parts. To get centimeters convert feet to inches before doing the calculations

centimeters = ((feet * 12) + inches) * 2.54.

meters = centimeters / 100.0

All 11 Replies

>>centimeters = (inches_per_foot * 2.5400);
To convert feet to centimeters feet * 30.48

I just tried and did not get the result I displayed in my first post. I want to put in 5 for feet and 8 and inches to recieve 1.7272 in meters and 172.72 in centimeters.

I just tried and did not get the result I displayed in my first post. I want to put in 5 for feet and 8 and inches to recieve 1.7272 in meters and 172.72 in centimeters.

Well, if you changed the code, please post the updated code.

#include <iostream>
#include <stdio.h>

using namespace std;

const double meters_per_foot = 0.3048;
const int centimeters_per_meter = 100;
const int inches_per_foot = 12;

void getValues(double& feet, double& inches);
void calculate(double feet,double inches, double& meters, double& centimeters);
void print(double feet,double inches,double meters,double centimeters);

int main ()
{

double feet=0,inches=0;
double meters=0,centimeters=0;
char letter;

getValues(feet,inches);
calculate(feet,inches,meters,centimeters);
print(feet,inches,meters,centimeters);

system ("PAUSE");
return 0;

}

void getValues(double& feet, double& inches)
{
cout<<"Enter the number of feet: "<<endl;
cin>>feet;
cout<<"Enter the number inches: "<<endl;
cin>>inches;
}
void calculate(double feet,double inches, double& meters, double& centimeters)
{

centimeters = (feet * 30.48);
meters = (inches * meters_per_foot);

}
void print(double feet,double inches,double meters,double centimeters)
{
cout<<feet<<" feet and "<<inches<<" inches  is equivalent to: "<<meters<<" meters  and "<<centimeters<<" centimeters."<<endl;
}

Feet and inches should be integers, not doubles because they have no fractional parts. To get centimeters convert feet to inches before doing the calculations

centimeters = ((feet * 12) + inches) * 2.54.

meters = centimeters / 100.0

centimeters = (feet * 30.48);
meters = (inches * meters_per_foot);

You need to use feet AND inches in each of these calculations, or convert inches to feet or vice versa and then convert to metric. Right now, 5 foot 8 ends up being:

meters = 8 * meters_per_foot

It needs to be:

meters = (# of feet in 5' 8" * meters_per_foot)

Thanks guy. The calculations that you gave me worked. I deeply appreciate it.

I have one more question... I want to do a do while loop so it can be continous until I press 'y'.. wherever i implement the do-while, i get errors. Where do I put it inside my code?

#include <iostream>
#include <stdio.h>

using namespace std;

const double meters_per_foot = 0.3048;
const int centimeters_per_meter = 100;
const int inches_per_foot = 12;

void getValues(double& feet, double& inches);
void calculate(double feet,double inches, double& meters, double& centimeters);
void print(double feet,double inches,double meters,double centimeters);

int main ()
{

double feet=0,inches=0;
double meters=0,centimeters=0;
char letter;

getValues(feet,inches);
calculate(feet,inches,meters,centimeters);
print(feet,inches,meters,centimeters);

system ("PAUSE");
return 0;

}

void getValues(double& feet, double& inches)
{
cout<<"Enter the number of feet: "<<endl;
cin>>feet;
cout<<"Enter the number inches: "<<endl;
cin>>inches;
}
void calculate(double feet,double inches, double& meters, double& centimeters)
{

centimeters = ((feet * 12) + inches) * 2.54;
meters = centimeters / 100.0;

}
void print(double feet,double inches,double meters,double centimeters)
{
cout<<feet<<" feet and "<<inches<<" inches  is equivalent to: "<<meters<<" meters  and "<<centimeters<<" centimeters."<<endl;
}

put the loop around lines 25-27.

something like this

int main ()
{

double feet=0,inches=0;
double meters=0,centimeters=0;
char letter;

do {

// start loop here

getValues(feet,inches);
calculate(feet,inches,meters,centimeters);
print(feet,inches,meters,centimeters);

system ("PAUSE");

cout << "Do you want to Exit! (y/n)?\n";

} while ( answer != 'y' );

return 0;

}

Thanks again for all your help!

Be a part of the DaniWeb community

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