Problem A: Distance conversion (20 points) Write a C+program that first asks you
ID: 3753478 • Letter: P
Question
Problem A: Distance conversion (20 points) Write a C+program that first asks you to enter a distance with units. Then the program should ask which distance you want to convert it to. You should display the desired unit conversion. The units your program should be able toc inches : "in" or "inches feet: "ft" or "feet" meters: "m" or "meters" centimeters: "cm" or "centimeters" onvert from/to are: Your program should be able to accept the input either as the shortened version right after the number (see example 1 or as the full name after a space (see example 2). This applies to both the units before and after the conversion. The final answer should be the converted amount without units. Use the following conversions: 12 inches 1 foot 1 foot 0.3048 meters meter 100 centimeters Example 1 (user input is underlined): Enter distance with units: 200m What do you want to convert this to? 7874.02 Example 2 (user input is underlined): Enter distance with units: What do you want to convert this to? 39.624Explanation / Answer
Please refer to code below:
I have made the code as modular as possible. Also here main question is to parse the string input into value and units.
Hope this Helps.
#include <iostream>
#include <string>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define INCHES 1
#define FEET 2
#define METERS 3
#define CENTIMETERES 4
double getValue(string input) {
string value;
int i = 0;
for (i = 0; input[i] != '' && (isdigit(input[i]) || input[i] == '.') ;i++) {
value[i] = input[i];
}
value[i] = '';
return stod(value);
}
string getUnits(string input) {
string units;
int i = 0;
for (i = 0; input[i] != '' && (isdigit(input[i]) || input[i] == '.' || input[i] == ' ') ;i++) {
}
units = input.substr (i, input.length());
return units;
}
int getUnitType(string input) {
if ((0 == input.compare("m") || (0 == input.compare("meters")))) {
return METERS;
}
if ((0 == input.compare("ft") || (0 == input.compare("feet")))) {
return FEET;
}
if ((0 == input.compare("in") || (0 == input.compare("inches")))) {
return INCHES;
}
if ((0 == input.compare("cm") || (0 == input.compare("centimeteres")))) {
return CENTIMETERES;
}
return 0;
}
void convert(string input, string conversionUnit) {
double value = getValue(input);
string units = getUnits(input);
cout << "value: " << value << endl;
cout << "units: " << units << endl;
int inputUnit = getUnitType(units);
int OutputUnit = getUnitType(conversionUnit);
if(inputUnit == OutputUnit) {
// no conversion as same units
cout << "Result: " << input << endl;
return;
}
if(inputUnit == INCHES && OutputUnit == FEET) {
// 12 inches = 1 foot
cout << "Result: " << (value / 12) << " ft" << endl;
return;
}
if(inputUnit == FEET && OutputUnit == INCHES) {
// 12 inches = 1 foot
cout << "Result: " << (value * 12) << " in" << endl;
return;
}
if(inputUnit == INCHES && OutputUnit == METERS) {
// 1 inch = 0.0254 m
cout << "Result: " << (value * 0.0254) << " m" << endl;
return;
}
if(inputUnit == METERS && OutputUnit == INCHES) {
// 1 meter = 39.3701 in
cout << "Result: " << (value * 39.3701) << " in" << endl;
return;
}
if(inputUnit == INCHES && OutputUnit == CENTIMETERES) {
// 1 inch = 2.54 cm
cout << "Result: " << (value * 2.54) << " cm" << endl;
return;
}
if(inputUnit == CENTIMETERES && OutputUnit == INCHES) {
// 1 cm = 0.393701 in
cout << "Result: " << (value * 0.393701) << " in" << endl;
return;
}
if(inputUnit == FEET && OutputUnit == METERS) {
// 1 foot = 0.3048 m
cout << "Result: " << (value * 0.3048) << " m" << endl;
return;
}
if(inputUnit == METERS && OutputUnit == FEET) {
// 1 m = 3.28084 ft
cout << "Result: " << (value * 3.28084) << " ft" << endl;
return;
}
if(inputUnit == FEET && OutputUnit == CENTIMETERES) {
// 1 ft = 30.48 cm
cout << "Result: " << (value * 30.48) << " cm" << endl;
return;
}
if(inputUnit == CENTIMETERES && OutputUnit == FEET) {
// 1 cm = 0.0328084 ft
cout << "Result: " << (value * 0.0328084) << " ft" << endl;
return;
}
if(inputUnit == METERS && OutputUnit == CENTIMETERES) {
// 1 m = 100 cm
cout << "Result: " << (value * 100) << " cm" << endl;
return;
}
if(inputUnit == CENTIMETERES && OutputUnit == METERS) {
// 1 cm = 0.01 m
cout << "Result: " << (value * 0.01) << " m" << endl;
return;
}
return;
}
int main() {
string input;
string conversionUnit;
cout << "Enter distance with units: ";
getline(std::cin, input);
cout << endl << "What do you want to convert this to ? ";
cin >> conversionUnit;
convert(input, conversionUnit);
return 0;
}
OUTPUT
Enter distance with units: 200m
What do you want to convert this to ? inches
value: 200
units: m
Result: 7874.02 in
Enter distance with units: 1.3 feet
What do you want to convert this to ? cm
value: 1.3
units: feet
Result: 39.624 cm
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.