Answer to Question #161803 in C++ for Anonymous

Question #161803

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!

1
Expert's answer
2021-02-13T05:04:09-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog