Question is:
Write a program that asks the user if he or she wants to convert lengths or weights. If the user chooses lengths, then 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. If the user chooses weights, a similar question about pounds, ounces, kilograms, and grams is asked. The program then performs the desired conversion. I wrote the following code:
#include<iostream>
using namespace std;
void inputData1(double& ft, double& inch);
void calcLength_m_cm(double& ft, double& inch, double& m, double& cm);
void outputData1(double m, double cm);
void inputData2(double& m, double& cm);
void calcLength_ft_inch(double& m, double& cm, double& ft, double& inch);
void outputData2(double ft, double inch);
void inputData3(double& pd, double& ou);
void calcWeight_kg_g(double& pd, double& ou, double& kg, double& g);
void outputData3(double kg, double g);
void inputData4(double& kg, double& g);
void calcWeight_pd_ou(double& kg, double& g, double& pd, double& ou);
void outputData4(double pd, double ou);
double feets, inches, meters, centimeters, pounds, ounces, kilograms, grams;
int main()
{
char ch;
do
{
int choice1, choice2;
cout<<"Enter 1 for converting lengths and 2 for converting weights = ";
cin>>choice1;
if(choice1 == 1)
{
cout<<"Enter 1 for coverting meters and centimeters from feets and inches\n"
<<"OR\n"
<<"Enter 2 for converting feets and inches from meters and centimeters\n"
<<">>";
cin>>choice2;
if(choice2 == 1)
{
inputData1(feets, inches);
outputData1(meters, centimeters);
}
else if(choice2 == 2)
{
inputData2(meters, centimeters);
outputData2(feets, inches);
}
else
{
cout<<"This input is invalid.\n"
<<"You can only choose from 1 or 2.";
}
}
else if(choice1 == 2)
{
cout<<"Enter 1 for converting kilograms and grams from poundes and ounces\n"
<<"OR\n"
<<"Enter 2 for converting pounds and ounces from kilograms and grams\n"
<<">>";
cin>>choice2;
if(choice2 == 1)
{
inputData3(pounds, ounces);
outputData3(kilograms, grams);
}
else if(choice2 == 2)
{
inputData4(kilograms, grams);
outputData4(pounds, ounces);
}
else
{
cout<<"This input is invalid.\n"
<<"You can only choose from 1 or 2.";
}
}
else
{
cout<<"This input is invalid.\n"
<<"You can only choose from 1 or 2.";
}
cout<<"\nEnter y to repeat: ";
cin>>ch;
} while (ch=='y' || ch=='Y');
}
void inputData1(double& ft, double& inch)
{
cout<<"Enter lenghts in feet and inches\n"
<<"Feet = ";
cin>>ft;
cout<<"Inches = ";
cin>>inch;
calcLength_m_cm(feets, inches, meters, centimeters);
}
void calcLength_m_cm(double& ft, double& inch, double& m, double& cm)
{
m = (ft*0.3048) + (inch*0.0234);
cm = m * 100;
}
void outputData1(double m, double cm)
{
cout<<"Meters = "<<m<<" m\n";
cout<<"Centimeters = "<<cm<<" cm\n";
}
void inputData2(double& m, double& cm)
{
cout<<"Enter length in meters and centimeters\n"
<<"Meter = ";
cin>>m;
cout<<"Centimeter = ";
cin>>cm;
calcLength_ft_inch(meters, centimeters, feets, inches);
}
void calcLength_ft_inch(double& m, double& cm, double& ft, double& inch)
{
ft = (m*3.28) + (cm*0.03281);
inch = ft*12;
}
void outputData2(double ft, double inch)
{
cout<<"Feets = "<<ft<<" ft\n";
cout<<"Inches = "<<inch<<" mn\n";
}
void inputData3(double& pd, double& ou)
{
cout<<"Enter weight in pounds and ounces:\n"
<<"Pounds = ";
cin>>pd;
cout<<"Ounces = ";
cin>>ou;
calcWeight_kg_g(pounds, ounces, kilograms, grams);
}
void calcWeight_kg_g(double& pd, double& ou, double& kg, double& g)
{
kg = (pd/2.2046) + (ou/35.2736);
g = kg*1000;
}
void outputData3(double kg, double g)
{
cout<<"Kilograms = "<<kg<<" kg\n";
cout<<"Grams = "<<g<<" g";
}
void inputData4(double& kg, double& g)
{
cout<<"Enter weight in kilograms and grams:\n"
<<"Kilograms = ";
cin>>kg;
cout<<"Grams = ";
cin>>g;
calcWeight_pd_ou(kilograms, grams, pounds, ounces);
}
void calcWeight_pd_ou(double& kg, double& g, double& pd, double& ou)
{
pd = (kg*2.2046) + (g*.0022046);
ou = pd*16;
}
void outputData4(double pd, double ou)
{
cout<<"Pounds = "<<pd<<" lb\n";
cout<<"Ounces = "<<ou;
}
Question also includes - Your program will have if-else statements embedded inside of if-else statements, but only in an indirect way. The outer if-else statement will include two function calls as its two branches. If you try to create a four way branch, you are probably on wrong track. You should only need to think about two-way branches(even though the entire program does ultimately branch into four cases).
I am beginner to cpp and only covered till functions. This question was in functions chapter from book Problem Solving with C++ . I have written the above code but not sure whether it is good enough or not. What improvements can be made?