Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prompt the user to enter an I for the conversion from inches to centimeters, a Q

ID: 3884286 • Letter: P

Question

Prompt the user to enter an I for the conversion from inches to centimeters, a Q for the conversion of quarts to liters, a P for pounds to kilograms, an M for miles to kilometers, or an O for ounces to grams. Use a "simple-if" (single-branched if) to check the user's input and when it is wrong output a message, prompt the user to re-enter the choice, and store the new value. Use a multiway branch to ask the user to enter the value for conversion, and calculate the metric equivalent. Use "simple ifs" (single-branched if's) to confirm the entered value to be converted is valid (you do not have to check it is a number). Remember that for now you should assume the user will not enter an incorrect value twice. The message for the invalid input of value to be converted should be something like "The number of quarts cannot be negative. Please re-enter the number of quarts to be converted to liters"

Explanation / Answer

#include<iostream>
using namespace std;
int main(){
cout << "I : inches -> centimeters Q : quarts -> liters P : pounds -> kilograms M : miles -> kilometers O : ounces -> grams ";
char c;
cout << "Enter option:";
cin >> c;
if(!(c=='I' || c=='Q' || c=='P' || c=='M' || c=='O')){
cout << "Invalid option, re-enter option" << endl;
cout << "Enter option:";
cin >> c;
}
int n;
if(c=='I'){
cout << "Enter inches:";
cin >> n;
if(n<1){
cout << "The number of inches cannot be negative. Please re-enter the number of inches to be converted to centimeters" << endl;
cout << "Enter inches:";
cin >> n;
}
cout << n << " inches = " << n*2.54 << " centimeters" << endl;
}
else if(c=='Q'){
cout << "Enter Quarts:";
cin >> n;
if(n<1){
cout << "The number of Quarts cannot be negative. Please re-enter the number of quarts to be converted to liters" << endl;
cout << "Enter Quarts:";
cin >> n;
}
cout << n << " Quarts = " << n*0.946 << " litres" << endl;
}
else if(c=='P'){
cout << "Enter pounds:";
cin >> n;
if(n<1){
cout << "The number of pounds cannot be negative. Please re-enter the number of pounds to be converted to kilograms" << endl;
cout << "Enter pounds:";
cin >> n;
}
cout << n << " pounds = " << n*0.45 << " kilograms" << endl;
}
else if(c=='M'){
cout << "Enter miles:";
cin >> n;
if(n<1){
cout << "The number of miles cannot be negative. Please re-enter the number of miles to be converted to kilometers" << endl;
cout << "Enter miles:";
cin >> n;
}
cout << n << " miles = " << n*1.609 << " kilometers" << endl;   
}
else if(c=='O'){
cout << "Enter ounces:";
cin >> n;
if(n<1){
cout << "The number of ounces cannot be negative. Please re-enter the number of ounces to be converted to grams" << endl;
cout << "Enter ounces:";
cin >> n;
}
cout << n << " ounces = " << n*28.34 << " grams" << endl;
}
return 0;
}