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

so confused...please help! In this exercise, you will create a program that disp

ID: 3812842 • Letter: S

Question


so confused...please help!

In this exercise, you will create a program that displays a measurement in either inches or centimeters. If necessary, create a new project named Introductory17 Project, and the folder The program should allow the user the choice of converting a measurement from inches to centimeters or vice versa. Use two void func tions: one for each different conversion type. Enter your instructions into a source file named Introductory 17 cpp. Also enter appropriate comments and any additional instructions required by the compiler. Test the application appropriately,

Explanation / Answer

The concept used here is pass by reference. Please find below the program: -

# include <iostream>

using namespace std;

void conversionToInches(double &cms);//prototype
void conversionToCms(double &inches);//prototype
void output (double inches,int x); //prototype

int main () //Input Function
{
   double convFact;
   bool flag =true;
   int d;
   while (flag) {
       cout<<"Main menu:"<<endl;
       cout<<"1. Convert inches to centimeters."<<endl;
       cout<<"2. Modify centimeters to inches."<<endl;
       cout<<"3. Exit."<<endl;
       cin>>d;
       if ((d<=0) || (d > 4)) {
           cout<<"Error! Selection is not valid."<<endl;
       }
       else {
           switch (d)
           {
               case 1:
                   cout<<"Enter the value in inches:"<<endl;
                   cin>>convFact;
                   // calling void function by passing a variable and then printing using same variable
                   conversionToCms(convFact);
                   output(convFact,0);
                   break;
               case 2:
                   cout<<"Enter the value in centimeters:"<<endl;
                   cin>>convFact;
                   conversionToInches(convFact);
                   output(convFact,1);
                   break;
               default:
                   flag = false;
               }
       }
   }

  
   return 0;
}
// Passing Centimeters by reference
void conversionToInches (double &cms)
{
   cms = cms/2.54;
}
// Passing Inches by reference
void conversionToCms (double &inches)
{
   inches *= 2.54;
}
// Program to print the values
void output (double meters,int x)
{
   if(x == 0) {
       cout << meters << " cm"<< endl;
   } else if(x == 1) {
       cout << meters << " inches"<< endl;
   }
  
}