Ok the conversion from feet and inches to meters and centimeters (meters being an int and the remainder being converted into cm), but how can I do just the opposite?
See here's the code for feet and inches to meters and cm and it works perfectly:
But when I try just the opposite for meters and cm to feet and inches, the feet is correct, but the cm is completely messed up.Code:#include <iostream> using namespace std; void userInput(double& feet, double& inches); void convertUStoMetric(double& meters, double& centimeters); void output(double meters, double centimeters); int main() { double unit, subunit; char redo = 'y'; while (redo == 'y' || redo == 'Y') { userInput(unit, subunit); convertUStoMetric(unit, subunit); output(unit, subunit); cout << "Would you like to run it again? Y/N: "; cin >> redo; cout << "\n"; } system("pause"); return 0; } void userInput(double& feet, double& inches) { cout << "Enter the feet and inches with a space in between each: "; cin >> feet; cin >> inches; cout << "\n"; } void convertUStoMetric(double& meters, double& centimeters) { double feet = meters; double inches = centimeters; meters = (feet + inches/12) * 0.3048; centimeters = (meters - (int)meters) * 100; //5 feet = 1.524 meters //.524 meters = 52.4 cm //6 inches = 15.24 cm //total = 1 meter and 67.64 cm meters = (int)meters; } void output(double meters, double centimeters) { cout << "The converted measurements are " << meters << " meters and " << centimeters << " centimeters."; cout << "\n\n"; }
If you want to try it out, put in 5 0, and you should get 16 feet and 0 inches. But when I put 5 6, I get some crazy numbers. The cm to inches conversion is working how it's supposed to.Code:#include <iostream> using namespace std; void userInput(double& meters, double& centimeters); void convertMetricToUS(double& feet, double& inches); void output(double feet, double inches); int main() { double unit, subunit; char redo = 'y'; while (redo == 'y' || redo == 'Y') { userInput(unit, subunit); convertMetricToUS(unit, subunit); output(unit, subunit); cout << "Would you like to run it again? Y/N: "; cin >> redo; cout << "\n"; } system("pause"); return 0; } void userInput(double& meters, double& centimeters) { cout << "Enter the meters and centimeters with a space in between each: "; cin >> meters; cin >> centimeters; cout << "\n"; } void convertMetricToUS(double& feet, double& inches) { float meters = feet; float centimeters = inches; feet = (meters + centimeters*2.54) / 0.3048; inches = (feet - (int)feet) / 100; feet = (int)feet; //5 meter = 16.404 199 475 feet //0.404199475 feet = 0.00404199 inches //6 centimeter = 2.362 204 724 4 inch //total = 16 feet and //onlineconversion.com } void output(double feet, double inches) { cout << "The converted measurements are " << feet << " feet and " << inches << " inches."; cout << "\n\n"; }
Any advice or tips on what I can do to fix this?
P.S. It's also not the exact opposite because I tried fixing it and failed, but I get essentially the same thing.



LinkBack URL
About LinkBacks




When the user no longer wants to convert more measurements, then use a break statement to break out of the do/while loop.