so confused...please help! In this exercise, you will create a program that disp
ID: 3812842 • Letter: S
Question
so confused...please help!
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;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.