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” Please use C++
Expert Answer
Program:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
char userChoice;
double valueToConvert;
double convertedValue;
//print the menu for the user
cout<<“For inches to centimeters, enter I”;
cout<<“nFor quarts to liters, enter Q”;
cout<<“nFor pounds to kilograms, enter P”;
cout<<“nFor miles to kilometers, enter M”;
cout<<“nFor ounces to grams, enter O”;
//prompt and read the user choice
cout<<“nnEnter your choice: “;
cin>>userChoice;
//check if the choice entered is valid or not using an if statement
if(!(userChoice == ‘I’ || userChoice == ‘i’ || userChoice == ‘Q’ || userChoice == ‘q’ || userChoice == ‘P’ || userChoice == ‘p’
|| userChoice == ‘M’ || userChoice == ‘m’ || userChoice == ‘O’ || userChoice == ‘o’))
{
//if choice is invalid ask the user to enter the value again
cout<<“nThat is an invalid choice. Re-enter the choice: “;
cin>>userChoice;
}
//if the user entered I
if(userChoice == ‘i’ || userChoice == ‘I’)
{
//prompt and read the number if inches to be converted
cout<<“nEnter the number of inches to be converted: “;
cin>>valueToConvert;
//check if value entered is valid or not
if(valueToConvert < 0)
{
//if not valid, show an error message and re-prompt
cout<<“nThe number of inches cannot be negative.”;
cout<<“nPlease re-enter the number of inches to be converted into centimeters: “;
cin>>valueToConvert;
}
//convert the entered number of inches to centimeters
convertedValue = valueToConvert * 2.54;
//display the result
cout<<“n”<<valueToConvert << ” inches = “<<convertedValue<<” centimeters”;
}
//if the user entered Q
if(userChoice == ‘Q’ || userChoice == ‘q’)
{
//prompt and read the number of quarts to be converted
cout<<“nEnter the number of quarts to be converted: “;
cin>>valueToConvert;
//check if value entered is valid or not
if(valueToConvert < 0)
{
//if not valid, show an error message and re-prompt
cout<<“nThe number of quarts cannot be negative.”;
cout<<“nPlease re-enter the number of quarts to be converted into liters: “;
cin>>valueToConvert;
}
//convert the entered number of quarts to liters
convertedValue = valueToConvert * 0.946353;
//display the result
cout<<“n”<<valueToConvert << ” quarts = “<<fixed << setprecision(3) << convertedValue<<” liters”;
}
//if the user entered P
if(userChoice == ‘P’ || userChoice == ‘p’)
{
//prompt and read the number of pounds to be converted
cout<<“nEnter the number of pounds to be converted: “;
cin>>valueToConvert;
//check if value entered is valid or not
if(valueToConvert < 0)
{
//if not valid, show an error message and re-prompt
cout<<“nThe number of pounds cannot be negative.”;
cout<<“nPlease re-enter the number of pounds to be converted into kilograms: “;
cin>>valueToConvert;
}
//convert the entered number of pounds to kilograms
convertedValue = valueToConvert * 0.453592;
//display the result
cout<<“n”<<valueToConvert << ” pounds = “<< fixed <<setprecision(3) << convertedValue <<” centimeters”;
}
//if the user entered M
if(userChoice == ‘M’ || userChoice == ‘m’)
{
//prompt and read the number of miles to be converted
cout<<“nEnter the number of miles to be converted: “;
cin>>valueToConvert;
//check if value entered is valid or not
if(valueToConvert < 0)
{
//if not valid, show an error message and re-prompt
cout<<“nThe number of miles cannot be negative.”;
cout<<“nPlease re-enter the number of miles to be converted into kilometers: “;
cin>>valueToConvert;
}
//convert the entered number of miles to kilometers
convertedValue = valueToConvert * 1.60934;
//display the result
cout<<“n”<<valueToConvert << ” miles = “<< fixed <<setprecision(3) << convertedValue <<” kilometers”;
}
//if the user entered O
if(userChoice == ‘O’ || userChoice == ‘o’)
{
//prompt and read the number of ounces to be converted
cout<<“nEnter the number of ounces to be converted: “;
cin>>valueToConvert;
//check if value entered is valid or not
if(valueToConvert < 0)
{
//if not valid, show an error message and re-prompt
cout<<“nThe number of ounces cannot be negative.”;
cout<<“nPlease re-enter the number of ounces to be converted into grams: “;
cin>>valueToConvert;
}
//convert the entered number of ounces to grams
convertedValue = valueToConvert * 28.3495;
//display the result
cout<<“n”<<valueToConvert << ” ounces = “<< fixed <<setprecision(3) << convertedValue <<” grams”;
}
return 0;
}
Output:
Note: As per the instructions we assumed that user doesn’t enter incorrect values twice