In: Computer Science
Write a program that converts the user's number into the desired unit of measurement. The user will pick the desired unit from a menu shown below. Ask the follow-up question (the number to convert) and do the math ONLY if the user picks a valid choice from the menu. You can see the conversions / multipliers needed for this program in the output section below.
Menu/Prompt:
Enter the number that corresponds to your desired unit
conversion from the choices below:
[1] Tablespoon (Tbsp) to Teaspoons (Tsp)
[2] Ounces (Oz) to Grams (g)
[3] Cups to Milliliters (ml)
Enter a number: 3
Possible Input/Output (Assume user enters the number 1 as the base unit)
Enter the number of tablespoons:
There are 3 teaspoons in 1 tablespoons
Enter the number of ounces:
There are 28.35 grams in 1 ounces
Enter the number of cups:
There are 236.6 ml in 1 cups
Invalid choice. Please run the program again.
Notes and Hints:
1) What programming structure is best for menus? Use that!
"Enter the number that corresponds to your desired unit
conversion from the choices below:\n";
"[1] Tablespoon (Tbsp) to Teaspoons (Tsp)\n"
"[2] Ounces (Oz) to Grams (g)\n"
"[3] Cups to Milliliters (ml)\n"
"Enter a number: ";
cout << endl;
"Enter the number of tablespoons: ";
cout <<endl;
"There are " << YOUR CODE HERE<< " teaspoons in " << YOUR CODE HERE << " tablespoons";
"Enter the number of ounces: ";
cout << endl;
"There are " << YOUR CODE HERE<< " grams in " <<YOUR CODE HERE<< " ounces";
"Enter the number of cups: ";
cout << endl;
"There are " << YOUR CODE HERE << " ml in " <<YOUR CODE HERE << " cups";
"Invalid choice. Please run the program again.";
CODE FOR THE FOLLOWING PROGRAM :-
#include <iostream>
using namespace std;
int main()
{
//Variables used in the program
int choice;
float tbsp,Oz,cups;
do{
//MENU for user to choose
cout<<"Enter the number that corresponds to your desired unit conversion from the choices below:\n";
cout<<"[1] Tablespoon (Tbsp) to Teaspoons (Tsp)\n";
cout<<"[2] Ounces (Oz) to Grams (g)\n";
cout<<"[3] Cups to Milliliters (ml)\n";
cout<<"Enter a number: ";
//If user chooses 1
cin>>choice;
if(choice==1){
cout<<"Enter the number of tablespoons: ";
cin>>tbsp;
//converting tablespoons to teaspoons
cout<<"There are " << tbsp*3<< " teaspoons in " << tbsp << " tablespoons";
cout<<endl;
}
//If user chooses 2
else if(choice==2){
cout<<"Enter the number of ounces: ";
cin>>Oz;
//converting Ounces to grams
cout<<"There are " << Oz*28.35<< " grams in " <<Oz<< " ounces";
cout<<endl;
}
//if user chooses 3
else if(choice==3){
cout<<"Enter the number of cups: ";
cin>>cups;
//Converting cups to ml
cout<<"There are " << cups*236.6 << " ml in " <<cups<< " cups";
cout << endl;
}
else {
cout<<"Invalid choice. Please run the program again.";
}
}while(choice==1 || choice==2 || choice==3);
return 0;
}
SCREENSHOT OF THE CODE AND SAMPLE RUN:-
SAMPLE RUN:-
HAPPY LEARNING