How would you go about writting this code in C or C++ ? Would there be a need of
ID: 3787495 • Letter: H
Question
How would you go about writting this code in C or C++ ?
Would there be a need of loops?
100% EH Sun Feb 5 1 56 58 AM Karina Q E Chrome File Edit View History Bookmarks People Window Help Karina C Secure blackboard unl.edu bbcswebdav/pid-3855 223-dt-content-rid-32468118 1/courses/CSCE-155E-150.1171 /hw2.pdf Q tr hw2.pdf 4. Unit Converter (convert.c) Write a menu driven unit convert program that handles temperature, distance, and mass unit conversion. More specifically your unit converter needs to handle miles to km, C to F, and pounds to kg. The inverse of each of these needs to be handled as well for example km to miles. The program will prompt for a choice between d, m, and t for the initial menu and then a sum menu with 1 and 2 for which way the conversion needs to go. Check for invalid menu input at each step. Hint: All real numbers are allowed for the units including negatives and decimal numbers. Enter desrired conversion d: Distance m Mass t: temperature Select temperature unit conversion type 1) OF 2) C to F input temperature in F 40 temperature in OC -40.000000Explanation / Answer
#include <iostream>
using namespace std;
int main() {
char conversion;
int option;
double fahrenheit,celcius,miles,kms,pounds,kg;
cout<<"Enter desired conversion";
cout<<" d: Distance ";
cout<<" m: Mass";
cout<<" t: Temperature ";
cin>>conversion;
switch(conversion)
{
case 'd':
cout<<" Select distance unit conversion type";
cout<<" (1) miles to kilometres";
cout<<" (2) kilometres to miles";
cin>>option;
switch(option)
{
case 1: cout<<" Enter distance in miles";
cin>>miles;
kms = 1.6*miles;
cout<<" Distance in kilometres = "<<kms;
break;
case 2: cout<<" Enter distance in kilometres";
cin>>kms;
miles = 0.6*kms;
cout<<" Distance in miles = "<<miles;
break;
default: cout<<"Invalid option";
break;
}
break;
case 'm': cout<<" Select Mass unit conversion type";
cout<<" (1) Kg to pounds";
cout<<" (2) pounds to kg ";
cin>>option;
switch(option)
{
case 1: cout<<" Enter mass in kg";
cin>>kg;
pounds = 2.2 * kg; //conversion formula
cout<<" Mass in pounds = "<<pounds<<" pounds";
break;
case 2: cout<<" Enter mass in pounds";
cin>>pounds;
kg = 0.45 * pounds; //conversion formula
cout<<" Mass in kg = "<<kg<<" kgs";
break;
default: cout<<"Invalid option";
break;
}
break;
case 't': cout<<" Select temperature unit conversion type";
cout<<" (1) F to C";
cout<<" (2) C to F";
cin>>option;
switch(option) //nested switch
{
case 1: cout<<" Enter temperature in Fahrenheit";
cin>>fahrenheit;
celcius = (fahrenheit - 32) * 5/9; //conversion formula
cout<<" Temperature in celcius = "<<celcius;
break;
case 2: cout<<" Enter temperature in Celcius";
cin>>celcius;
fahrenheit = (celcius * 9/5) + 32;
cout<<" Temperature in fahrenheit = "<<fahrenheit;
break;
default: cout<<"Invalid option";
break;
}
break;
default:
cout<<"Invalid convertion";
break;
}
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.