Conversion Program with Pass by Value and Pass by Reference
May 8, 2013 at 7:49pm UTC
Hi, this site has been extremely helpful with my path down learning C++ but I have ran into an issue on one of my assignments. Below is my code I wrote to create a basic program for conversions. My assignment now is to modify the program so that each calculation is performed by a user defined function. At least one of the user defined functions will utilize the "pass by value" method and at least one using the "pass by reference" method.
I am a little confused on how to start this and how it should exactly look like. If anyone could please provide me an example of how I should start, or some basic syntax so I can grasp the concept of how I should do this I would greatly appreciate it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include<iostream> //Required for cout
#include<iomanip>
using namespace std;
int main ()
{
cout << "Which way would you like to convert?\n" <<endl ;
char value;
cout << "1. English to Metic" <<endl ;
cout << "2. Metric to English\n" <<endl ;
cout << "Enter Corresponding Number: " ;
cin >> value;
switch (value) {
case '1' : // Case for English to Metric
int code;
double value, result;
double conv_centimeters(2.54), conv_liters(3.787), conv_newtons(4.448);
cout << "\nWhat would you like to convert?\n" ;
cout << "1. Fahrenheit to Celsius " << endl;
cout << "2. Inches to Centimeters " << endl;
cout << "3. Gallons to Liters " << endl;
cout << "4. Pounds Force to Newtons " << endl;
cout << "\nEnter Corresponding Number: " ;
cin >> code;
cout << "\nEnter the value to convert: " ;
cin >> value;
if (code == 1) {
result = (value-32)*5.0/9.0;
cout << value << " Degrees Farenheit is equal to " << setprecision(4) << result << " Degrees Celsius\n" ;
}
if (code == 2) {
result = value*conv_centimeters;
cout << value << "Inches are equal to " << setprecision(4) << result << " Centimeters\n" ;
}
if (code == 3) {
result = value*conv_liters;
cout << value << " Gallons are equal to " << setprecision(4) << result << " Liters\n" ;
}
if (code == 4) {
result = value*conv_newtons;
cout << value << " Pound Forces are equal to " << setprecision(4) << result << " Newtons\n" ;
}
}
switch (value) {
case '2' : // Case for Metric To English
int code;
double value, result;
const double conv_inches(0.39), conv_gallons(0.26), conv_poundsforce(0.22);
cout << "\nWhat would you like to convert?\n" ;
cout << "1. Celsius to Fahrenheit " << endl;
cout << "2. Centimeters to Inches " << endl;
cout << "3. Liters to Gallons " << endl;
cout << "4. Newtons to Pound Forces " << endl;
cout << "\nEnter Corresponding Number: " ;
cin >> code;
cout << "\nEnter the value to convert: " ;
cin >> value;
if (code == 1) {
result = value*1.8+32;
cout << value << " Degrees Celsius is equal to " << setprecision(4) << result << " Degrees Fahrenheit\n" ;
}
if (code == 2) {
result = value*conv_inches;
cout << value << " Centimeters are equal to " << setprecision(4) << result << " Inches\n" ;
}
if (code == 3) {
result = value*conv_gallons;
cout << value << " Liters are equal to " << setprecision(4) << result << " Gallons\n" ;
}
if (code == 4) {
result = value*conv_poundsforce;
cout << value << " Newtons are equal to " << setprecision(4) << result << " Pound Forces\n" ;
}
}
cout << "\nConversion complete!! \n" << endl;
system ("pause" );
return 0;
}
Topic archived. No new replies allowed.