(You should do the previous two programming projects before doing this one.) Write a program
that combines the functions in the previous two programming projects. The program asks
the user if he or she wants to convert from feet and inches to meters and centimeters or from
meters and centimeters to feet and inches. The program then performs the desired conversion.
Have the user respond by typing the integer 1 for one type of conversion and 2 for the other
conversion. The program reads the user’s answer and then executes an if-else statement.
Each branch of the if-else statement will be a function call. The two functions called in the
if-else statement will have function definitions that are very similar to the programs for the
previous two programming projects. Thus, they will be fairly complicated function definitions that
call other functions in their function bodies. Include a loop that lets the user repeat this computation
for new input values until the user says he or she wants to end the program.
PROGRAM CODE:
//Header file section
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Function to convert feet and inches to meters and centimeters
void feetInchesToMetersCentimeters()
{
double feet, inches;
const double METERS_PER_FOOT = 0.3048;
const double CENTIMETERS_PER_INCH = 2.54;
// Promtp and read the length in feet and inches from the user
cout << "Enter the length in feet: ";
cin >> feet;
cout << "Enter the length in inches: ";
cin >> inches;
// Convert feet and inches to meters and centimeters
double totalInches = 12 * feet + inches;
double meters = totalInches * CENTIMETERS_PER_INCH / 100.0;
int centimeters = static_cast<int>(round((meters - floor(meters)) * 100));
// Output the result
cout << "Equivalent length in meters and centimeters: " << floor(meters) << " meters, "
<< centimeters << " centimeters" << endl;
}
// Function to convert meters and centimeters to feet and inches
void metersCentimetersToFeetInches() {
double meters, centimeters;
const double FEET_PER_METER = 3.28084;
const double INCHES_PER_CENTIMETER = 0.393701;
// Get the length in meters and centimeters from the user
cout << "Enter the length in meters: ";
cin >> meters;
cout << "Enter the length in centimeters: ";
cin >> centimeters;
// Convert meters and centimeters to feet and inches
double totalInches = (meters * 100 + centimeters) * INCHES_PER_CENTIMETER;
double feet = floor(totalInches / 12);
int inches = static_cast<int>(round(totalInches - feet * 12));
// Output the result
cout << "Equivalent length in feet and inches: " << feet << " feet, "
<< inches << " inches" << endl;
}
int main() {
int choice;
// Loop until the user says they want to end the program
while (true) {
// Ask the user for the conversion type
cout << "Choose the conversion type:" << endl;
cout << "1. Feet and inches to meters and centimeters" << endl;
cout << "2. Meters and centimeters to feet and inches" << endl;
cout << "Enter 1 or 2 (or any other number to exit): ";
cin >> choice;
if (choice == 1) {
// Perform the feet and inches to meters and centimeters conversion
feetInchesToMetersCentimeters();
} else if (choice == 2) {
// Perform the meters and centimeters to feet and inches conversion
metersCentimetersToFeetInches();
} else {
// Exit the loop for any other input
break;
}
}
return 0;
}
OUTPUT:
Choose the conversion type:
1. Feet and inches to meters and centimeters
2. Meters and centimeters to feet and inches
Enter 1 or 2 (or any other number to exit): 1
Enter the length in feet: 222
Enter the length in inches: 333
Equivalent length in meters and centimeters: 76 meters, 12 centimeters
Choose the conversion type:
1. Feet and inches to meters and centimeters
2. Meters and centimeters to feet and inches
Enter 1 or 2 (or any other number to exit): 2
Enter the length in meters: 500
Enter the length in centimeters: 100
Equivalent length in feet and inches: 1643 feet, 8 inches
Choose the conversion type:
1. Feet and inches to meters and centimeters
2. Meters and centimeters to feet and inches
Enter 1 or 2 (or any other number to exit): 4