NEED HELP with my Function !!!

Started by
0 comments, last by djacq 22 years, 9 months ago
I need desperate help with solving a function problem that is stressing me out. How do you return an increment value from a function back into the main. Here''s a sample: #include #include void process_deposits (double, double, int, double); void process_deposits (double new_bal, double amount, int n_dep, double total_dep) { cout << "Enter the deposit amount : $ "; cin >> amount; new_bal += amount; ++n_dep; total_dep += amount; cout << "New balance : $ " << new_bal << " "; } int main (void) { while (tranx_type != ''Q'' && tranx_type != ''q'') { if (tranx_type == ''D'' || tranx_type == ''d'') { process_deposits (new_bal, amount, n_dep, total_dep); } else if (tranx_type == ''Q'' || tranx_type == ''q'') { cout << n_dep << " Deposits Totaling $ " << setw(8) << total_dep << "\n"; } }
Advertisement
*reformats code so it is actually readable*

      #include < iostream.h >#include < iomanip.h >			void process_deposits (double, double, int, double);void process_deposits (double new_bal, double amount, int n_dep, double total_dep){  cout << "Enter the deposit amount : $ ";  cin  >> amount;  new_bal += amount;  ++n_dep;	  total_dep += amount;  cout << "New balance : $ " << new_bal << " ";}int main (void){  while (tranx_type != 'Q' && tranx_type != 'q')  {     if (tranx_type == 'D' || tranx_type == 'd')     { 	process_deposits (new_bal, amount, n_dep, total_dep);     }	     else if (tranx_type == 'Q' || tranx_type == 'q')     {  	cout << n_dep << " Deposits Totaling      $ " << setw(8) << total_dep << "\n";     }}  


Right.... the problem is that the variables that you pass to process_deposits are being copied. You are incrementing the copies, and then as soon as the function exits the incremented values are thrown away. There are several solutions to this problem....

1. Use a return value from process_deposits to give you the new value. Problem: you can only return one piece of data, so all the other things will still be lost.

2. Make all the things you want to change in process_deposits global. Problem: heavy use of globals is evil. (although for a simple app like this it is fine)

3. Pass the data that needs to be changed to the function using pointers. Problem: if you dont know what you are doing with pointers you will probably get lots of nasty crashes and errors and stuff.

4. Pass the data by reference. Problem: Only works in C++, but since you are using C++, that shouldnt be a problem. This is probably the best solution. Change your definition of process_deposits to this...

        // ps... your prototype needs changing too, or just removing.// as posted you dont need to prototype this functionvoid process_deposits (double& new_bal, int& n_dep, double& total_dep){  double amount; // dont need to pass this in, it is only used here  // code goes here}     



Edited by - Sandman on August 8, 2001 2:16:30 PM

This topic is closed to new replies.

Advertisement