### Pass by Reference HELP PLEASE

Okay so in the code bellow I was suppose to use pass by reference though this was not listed in the directions I was given.....could someone please show me how to adjust my code so that it uses pass by reference!? Any help would be greatly appreciated!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229`` ``````/************************************************************************************ Purpose: The purpose of this program was to take the program from 4b and add functions to convert the values entered into another type of measurement. The program asks the user which type of conversion they would like to convert, and then converts it, finally outputting the final conversions to the screen. ************************************************************************************/ #include using namespace std; void convert_lengths(); void convert_weights(); int round(double x); //rounds off decimal places void weight_to_metric(); double input_the_lbsandoz(); double convert_the_lbsandoz(); void output_the_kgandg(); //conversion functions for us to metric weight void weight_to_us(); double input_the_kgandg(); double convert_the_kgandg(); void output_the_lbsandoz(); //conversion functions for metric to us weight void length_to_metric(); double input_the_ftandin(); double convert_the_ftandin(); void output_the_mandcm(); //conversion functions for us to metric length void length_to_us(); double input_the_mandcm(); double convert_the_msandcm(); void output_the_ftandin(); //conversion functions for metric to us length int main() { int ans; do { cout << "This program converts lengths and weights." << endl; cout << "Press 1 to convert lengths." << endl; cout << "Press 2 to convert weights." << endl; cout << "Press 0 to end the program." << endl; cout << "Enter: "; cin >> ans; if (ans == 1) { convert_lengths(); } else if (ans == 2) { convert_weights(); } else if (ans == 0) cout << "Program terminated." << endl; else cout << "Please enter a vaild choice." << endl; } while (ans > 2 || ans < 0); return 0; } void convert_lengths() { int answer; do { cout << endl; cout << "You have chosen to convert lengths." << endl; cout << "If you would like to convert ft./in. to m/cm press 1." << endl; cout << "If you would like to convert m/cm to ft./in. press 2." << endl; cout << "If you would like to stop converting lengths press 0." << endl; cout << "Enter: "; cin >> answer; if (answer == 1) length_to_metric(); //conversion function ft/in to m/cm else if (answer == 2) length_to_us(); //conversion function m/cm to ft/in else if (answer == 0) main(); else cout << "Please enter a valid choice." << endl; } while (answer < 0 || answer > 2); } void convert_weights() { int ans; do { cout << endl; cout << "If you would like to convert lbs./oz. to kg/g press 1." << endl; cout << "If you would like to convert kg/g to lbs./oz. press 2." << endl; cout << "If you would like to stop converting weights press 0." << endl; cout << "Enter: "; cin >> ans; if (ans == 1) weight_to_metric(); //weight to metric function else if (ans == 2) weight_to_us(); //weight to us function else if (ans == 0) main(); else cout << "Please enter a valid choice." << endl; } while (ans < 0 || ans > 2); } void length_to_metric() { cout << "You have chosen to convert from us to metric lengths." << endl; output_the_mandcm(); } void length_to_us() { cout << "You have chosen to convert from metric to us." << endl; output_the_ftandin(); } void weight_to_metric() { cout << "You have chosen to convert from us to metric weights." << endl; output_the_kgandg(); } void weight_to_us() { cout << "You have chosen to convert from metric to us." << endl; output_the_lbsandoz(); } double input_the_ftandin() { int feet, inch; double totalin; cout << "Please enter the number of feet: "; cin >> feet; cout << "Please enter the number of inches: "; cin >> inch; totalin = inch + (feet * 12); return totalin; } double convert_the_ftandin() { double totalcm, totalmeters, totalin = input_the_ftandin(); totalcm = totalin * 2.54; totalmeters = totalcm / 100; return totalmeters; } void output_the_mandcm() { double totalmeters = convert_the_ftandin(), totalcm; totalcm = round(totalmeters * 100) % 100; totalmeters = floor(totalmeters); cout << "The total meters are: " << totalmeters << endl; cout << "The total centimeters are: " << totalcm << endl; } double input_the_mandcm() { double meters, cm; cout << "Please enter the number of meters: "; cin >> meters; cout << "Please enter the number of cm: "; cin >> cm; meters = meters + (cm / 100); return meters; } double convert_the_mandcm() { double totalfeet, totalmeters = input_the_mandcm(); totalfeet = totalmeters / .3048; return totalfeet; } void output_the_ftandin() { double totalinches, totalfeet = convert_the_mandcm(); totalinches = totalfeet * 12; totalinches = round(totalinches) % 12; cout << "The total feet are: " << floor(totalfeet) << endl; cout << "The total inches are: " << totalinches << endl; } double input_the_lbsandoz() { double lbs, oz; cout << "Please enter the number of lbs: "; cin >> lbs; cout << "Please enter the number of oz.: "; cin >> oz; lbs = lbs + (oz / 16); return lbs; } double convert_the_lbsandoz() { double totallbs = input_the_lbsandoz(), totalkg, totalgrams; totalkg = totallbs / 2.2046; totalgrams = totalkg * 1000; return totalgrams; } void output_the_kgandg() { double totalkg, totalgrams = convert_the_lbsandoz(); totalkg = floor(totalgrams / 1000); totalgrams = round(totalgrams) % 1000; cout << "The total kilograms are: " << totalkg << endl; cout << "The total grams are: " << totalgrams << endl; } double input_the_kgandg() { double kg, g; cout << "Please enter the number of Kg: "; cin >> kg; cout << " Please enter the number of grams: "; cin >> g; kg = kg + (g / 1000); return kg; } double convert_the_kgandg() { double totallbs, totalkg = input_the_kgandg(); totallbs = 2.2046 * totalkg; return totallbs; } void output_the_lbsandoz() { double totallbs = convert_the_kgandg(), totaloz; totaloz = totallbs * 16; totaloz = round(totaloz) % 16; totallbs = floor(totallbs); cout << "The total pounds are: " << totallbs << endl; cout << "The total ounces are: " << totaloz << endl; } int round(double x) { if(x - floor(x) < 0.5) return floor(x); else return ceil(x); } ``````
http://www.cplusplus.com/doc/tutorial/functions2/
Go to that page, and read what pass by reference is. It will benefit you much more to understand the repercussions of pass by reference, rather than just know how to apply it.
I have read it but how can I adjust this code without scraping the whole program!!!???
You seem to rarely pass variables, most functions are subroutines that return a value. The exception being round(double):

 ``1234`` ``````int round(double &x) { // ... }``````

You now pass a double by reference.

You can also return by reference, but I think you must input something by reference or return a reference to a newed pointer, else the referenced object is deleted from the stack of the function.
Could you show me how to properly pass a variable with the given code that I have above so that I can better understand? thank you for your help bourgond aries and spring08 though :) I am just not sure how to fix the issue still or if its even possible to keep the code I have and rectify the problem...

 ``123`` ``````void convert_the_kgandg(double& totallbs) { double totallbs, totalkg = input_the_kgandg(); totallbs = 2.2046 * totalkg;``````