In: Computer Science
Write a menu-driven program to test the three functions
conver_tlength(), convert_width(), convert_volume() in a program.
Program should allow the user to select one of the options
according to whether lengths, weights or volume are to be
converted, read the volue to be converted and the units, and then
call the appropriate function to carry out the conversion
In unit out unit
I C (inch to centimeter 1 in = 2.4 cm)
F C (feet to centimeter 1 ft = 30.4 cm)
F M (feet to meter 1 ft = 0.304 m)
Y M (yard to centimeter 1 in = 2.4 m)
M K (miles to kilometer 1 mi = 1.60 km)
O G (ounces to grams 1 oz = 28.34 g)
P K (pounds to kilograms 1 LB = .4 kg)
P L (pints to centimeter 1 PT = 0.47 L)
Q L (quarts to liters 1 QT = 0.94 L)
G L (gallons to liters 1 gal = 3.7 L)
NEED THIS IN C++
Here is the solution,
// your code starts here
#include <iostream>
using namespace std;
int main()
{
// variable declarations
int choice;
float inches,feet,yard,pints;
float miles,ounces,pounds,quarts,gallons;
// function declarations
// function for printing menu
void printMenu();
// function to convert to length
void convert_length(float,string,string);
// function to convert to weight
void convert_weight(float,string);
// function to convert to volumn
void convert_volumn(float,string,string);
// loop until users chose to exit
do {
cout<<endl<<endl;
printMenu();
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
// accept inches
cout<<"Enter Inches:";
cin>>inches;
// call to convert_length function
convert_length(inches,"inches","centimeter");
break;
case 2:
// accept feet
cout<<"Enter feets:";
cin>>feet;
// call to convert_length function
convert_length(feet,"feet","centimeter");
break;
case 3:
// accept feets
cout<<"Enter feets:";
cin>>feet;
// call to convert_length function
convert_length(feet,"feet","meter");
break;
case 4:
// accept yards
cout<<"Enter Yards:";
cin>>yard;
// call to convert_length function
convert_length(yard,"yard","centimeter");
break;
case 5:
// accept miles
cout<<"Enter Miles:";
cin>>miles;
// call to convert_length function
convert_length(miles,"miles","kilometer");
break;
case 6:
// accept ounces
cout<<"Enter Ounces:";
cin>>ounces;
// call to convert_weight function
convert_weight(ounces,"grams");
break;
case 7:
// accept pounds
cout<<"Enter Pounds:";
cin>>pounds;
// call to convert_weight function
convert_weight(pounds,"kilograms");
break;
case 8:
// accept Pints
cout<<"Enter Pints:";
cin>>pints;
// call to convert_volumn function
convert_volumn(pints,"pints","liter");
break;
case 9:
// accept Quarts
cout<<"Enter Quarts:";
cin>>quarts;
// call to convert_volumn function
convert_volumn(quarts,"quarts","liter");
break;
case 10:
// accept Gallons
cout<<"Enter Gallons:";
cin>>gallons;
// call to convert_volumn function
convert_volumn(gallons,"gallons","liter");
break;
case 11:
exit(0);
break;
}
}while(choice!=11);
return 0;
}
void printMenu()
{
cout<<"1) Convert Inches to centimeters" <<endl;
cout<<"2) Convert feet to centimeters" <<endl;
cout<<"3) Convert feet to meter" <<endl;
cout<<"4) Convert yard to centimeter" <<endl;
cout<<"5) Convert miles to kilometer"<<endl;
cout<<"6) Convert ounces to grams" <<endl;
cout<<"7) Convert pounds to kilograms" <<endl;
cout<<"8) Convert pints to liters" <<endl;
cout<<"9) Convert quarts to liters" <<endl;
cout<<"10) Convert gallons to liters" <<endl;
cout<<"11) Exit"<<endl;
}
void convert_length(float value,string s1,string s2)
{
// inches to centimeter
if(s1=="inches" && s2 == "centimeter")
{
cout<<"Centimeter is : "<<value * 2.54;
}
// feet to centimeter
else if(s1=="feet" && s2 == "centimeter")
{
cout<<"Centimeter is : "<<value * 30.4;
}
// feet to meter
else if(s1=="feet" && s2 == "meter")
{
cout<<"Meter is : "<<value * 0.304 ;
}
// yard to centimeter
else if(s1=="yard" && s2 == "centimeter")
{
cout<<"Centimeter is : "<<value * 91.44;
}
// miles to kilometer
else if(s1=="miles" && s2 == "kilometer")
{
cout<<"Kilometer is : "<<value * 1.60;
}
}
void convert_weight(float value,string s)
{
// ounces to grams
if(s == "grams")
{
cout<<"Grams is : "<<value * 28.34;
}
// pounds to kilogram
else if (s == "kilograms") {
cout<<"Kilograms is : "<<value * 0.4;
}
}
void convert_volumn(float value,string s1,string s2)
{
// pints to liter
if(s1=="pints" && s2 == "liter")
{
cout<<"Liter is : "<<value * 0.47;
}
// quarts to liter
else if(s1=="quarts" && s2 == "liter")
{
cout<<"Liter is : "<<value * 0.94;
}
// gallons to liter
else if(s1=="gallons" && s2 == "liter")
{
cout<<"Liter is : "<<value * 3.78;
}
}
// code ends here
here is the sample output:-
here is the screenshot of the code:-
Thank You.