meters 2 ft/in problem

D

DrNoose

Hi!

I'm just getting started learning c++. I have a program that is driving
me nuts! I think I have it working all but one portion. Basically, you
input a number of "meters" as a double. The program is supposed to
convert the meters into feet & inches. I understand that to get the feet
part - meters (in this case meters is a pass by reference) is to be
multiplied by 3.2808. I know I should have something like feet = ***,
but I get an error because I'm mixing int's with doubles?

Here is my program so far. It should have the following input/output:

"Enter a number of meters as a double"
5.00
The value of 5.00 meters and centimeters converted to English measure is
16 feet, 4.85 inches.

"Y or y continues, any other character quits." (if you choose "other
charc." then it says "Goodbye!"

I think my problem is in the void convert function.
This is the output that I usually get:

"The value of meters, centimeters 5.00 converted to English measure is
-4194884 feet, 1.37 inches.
Y or y to continues, any other character quits

Any help is greatly appreciated!

Thanks!

Kathy

//********************************************************************************************
//
// Meters&CentemetersToFeet&Inches.cpp
//
// This program prompts the user for a length separated into the meters
and centimeters,
// converts the length into feet and inces and displays the converted length
// to the user. The user is given the opportunity to repeat the conversion.
//
//
//********************************************************************************************

#include <iostream>
#include <iomanip>
using namespace std;

void input(double& meters);
void convert(int& feet, double& inches, double meters);
void output(int feet, double inches, double meters);

int main()
{

int feet;
double inches, meters;
char ans;

do
{
input(meters);
convert(feet, inches, meters);
output(feet, inches, meters);
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;

} while('Y' == ans || 'y' == ans);

cout << endl;
cout << "Goodbye!\n";


return 0;
}

void input(double& meters)
{
cout << "Enter a number of meters as a double \n";
cin >> meters;
}

void convert(int &feet, double& inches, double meters)
{

inches = (meters * 3.2808) / 12;

}
void output(int feet, double inches, double meters)
{

cout << fixed << showpoint;
cout << setprecision(2);
cout <<"The value of meters, centimeters " << meters << endl;
cout << " converted to English measure is " << feet << " feet, "
<< inches << " inches" << endl;

}
 
H

Howard

DrNoose said:
Hi!

I'm just getting started learning c++. I have a program that is driving me
nuts! I think I have it working all but one portion. Basically, you input
a number of "meters" as a double. The program is supposed to convert the
meters into feet & inches. I understand that to get the feet part - meters
(in this case meters is a pass by reference) is to be multiplied by
3.2808. I know I should have something like feet = ***, but I get an error
because I'm mixing int's with doubles?

Here is my program so far. It should have the following input/output:
void convert(int &feet, double& inches, double meters)
{

inches = (meters * 3.2808) / 12;

You're saying that the number of inches is equal to 1/12th the total number
of feet? Hardly. Check your formula for getting feet-and-inches from total
feet.

Where do you set the value for feet???

If it never gets set, then it can be any number, and the behavior is
undefined. You need to calculate the feet here.

You say you tried but got an error. Well, what code did you try? What was
the error?

You should probably write your the formula you trying to use in comments
above the function. That will help you see if the function actually does
what the formula says it does.

-Howard
 
G

Greg

DrNoose said:
Hi!

I'm just getting started learning c++. I have a program that is driving
me nuts! I think I have it working all but one portion. Basically, you
input a number of "meters" as a double. The program is supposed to
convert the meters into feet & inches. I understand that to get the feet
part - meters (in this case meters is a pass by reference) is to be
multiplied by 3.2808. I know I should have something like feet = ***,
but I get an error because I'm mixing int's with doubles?

Here is my program so far. It should have the following input/output:

"Enter a number of meters as a double"
5.00
The value of 5.00 meters and centimeters converted to English measure is
16 feet, 4.85 inches.

"Y or y continues, any other character quits." (if you choose "other
charc." then it says "Goodbye!"

I think my problem is in the void convert function.
This is the output that I usually get:

"The value of meters, centimeters 5.00 converted to English measure is
-4194884 feet, 1.37 inches.
Y or y to continues, any other character quits

Any help is greatly appreciated!

Thanks!

Kathy

//********************************************************************************************
//
// Meters&CentemetersToFeet&Inches.cpp
//
// This program prompts the user for a length separated into the meters
and centimeters,
// converts the length into feet and inces and displays the converted length
// to the user. The user is given the opportunity to repeat the conversion.
//
//
//********************************************************************************************

#include <iostream>
#include <iomanip>
using namespace std;

void input(double& meters);
void convert(int& feet, double& inches, double meters);
void output(int feet, double inches, double meters);

int main()
{

int feet;
double inches, meters;
char ans;

do
{
input(meters);
convert(feet, inches, meters);
output(feet, inches, meters);
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;

} while('Y' == ans || 'y' == ans);

cout << endl;
cout << "Goodbye!\n";


return 0;
}

void input(double& meters)
{
cout << "Enter a number of meters as a double \n";
cin >> meters;
}

void convert(int &feet, double& inches, double meters)
{

inches = (meters * 3.2808) / 12;

}
void output(int feet, double inches, double meters)
{

cout << fixed << showpoint;
cout << setprecision(2);
cout <<"The value of meters, centimeters " << meters << endl;
cout << " converted to English measure is " << feet << " feet, "
<< inches << " inches" << endl;

}

Since this program calculates a single value, it needs only one
variable to hold that value. This program runs into trouble because it
tries to use two variables (feet, inches) instead of one (inches) to
represent the result. This approach is error-prone, as the program
demonstrates when it then neglects to set one of the two variables.
Using a single variable reduces the chance for error. So I would change
convert to:

const int kInchesPerFoot = 12;
const double kFeetPerMeter = 3.2808;

double convertToInches( double meters)
{
return meters * kFeetPerMeter * kInchesPerFoot;
}

and then change the output text:

cout <<"The value of meters, centimeters " << meters << endl;
cout << " converted to English measure is " <<
int(inches)/kInchesPerFoot << " feet, "
<< inches << " int(inches) % kInchesPerFoot" << endl;

Greg
 
J

Jim Langston

DrNoose said:
Hi!

I'm just getting started learning c++. I have a program that is driving me
nuts! I think I have it working all but one portion. Basically, you input
a number of "meters" as a double. The program is supposed to convert the
meters into feet & inches. I understand that to get the feet part - meters
(in this case meters is a pass by reference) is to be multiplied by
3.2808. I know I should have something like feet = ***, but I get an error
because I'm mixing int's with doubles?

Here is my program so far. It should have the following input/output:

"Enter a number of meters as a double"
5.00
The value of 5.00 meters and centimeters converted to English measure is
16 feet, 4.85 inches.

"Y or y continues, any other character quits." (if you choose "other
charc." then it says "Goodbye!"

I think my problem is in the void convert function.
This is the output that I usually get:

"The value of meters, centimeters 5.00 converted to English measure
is -4194884 feet, 1.37 inches.
Y or y to continues, any other character quits

Any help is greatly appreciated!

Thanks!

Kathy

//********************************************************************************************
//
// Meters&CentemetersToFeet&Inches.cpp
//
// This program prompts the user for a length separated into the meters
and centimeters,
// converts the length into feet and inces and displays the converted
length
// to the user. The user is given the opportunity to repeat the
conversion.
//
//
//********************************************************************************************

#include <iostream>
#include <iomanip>
using namespace std;

void input(double& meters);
void convert(int& feet, double& inches, double meters);
void output(int feet, double inches, double meters);

int main()
{

int feet;
double inches, meters;
char ans;

do
{
input(meters);
convert(feet, inches, meters);
output(feet, inches, meters);
cout << "Y or y continues, any other character quits "
<< endl;
cin >> ans;

} while('Y' == ans || 'y' == ans);

cout << endl;
cout << "Goodbye!\n";


return 0;
}

void input(double& meters)
{
cout << "Enter a number of meters as a double \n";
cin >> meters;
}

void convert(int &feet, double& inches, double meters)
{

inches = (meters * 3.2808) / 12;

}

Your formula is wrong because of a number of things.
1. You don't set feet.
2. That is total inches, not the remaining inches.

You have the total number of inches. Now calculate how many feet this is
with any remaining inches. 12 inches in a foot. Then set your foot and
inches variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top