This application sorts occupations by salary and prints these values to a sorted table. It also calculates the average and median salary based on the input data, then prints these numbers out to a text file.
My problem is that I can't figure out how to get the values for avgSalary and medSalary from the calc function to the function call in main(). Probably something simple but have been stuck on this for a while.
Use references. Change: double calc(double avgSalary, double medSalary)
to: double calc(double& avgSalary, double& medSalary)
then modifying the parameters in calc(..) will modify them in the caller.
As it is, the parameters are copied onto the stack and lost when the function returns.
Alrtight thanks. I looked at the link you posted and changed double calc(...) but I still keep getting 0.00 for the average and the median in the output file.
I haven't had a detailed look at your code, but the following function will definitely not work: double calc(double avgSalary, double medSalary) on line 104. It's called from line 58.
The problem is that the array on line 106 is not the same as the array on line 19. In fact it is uninitialised, so your answers should be 'garbage'.
Further the sum on line 107 is never computed; it's always zero. But you are calculating the sum on line 47, so you can pass this in.
You need to pass in the salary array (defined on line 19), and remove the definition on line 106.
So change: double calc(double& avgSalary, double& medSalary) on line 104
to: double calc(double sum, double salary[], double& avgSalary, double& medSalary)
and change: calc(avgSalary, medSalary); on line 58
to: calc(sum,salary,avgSalary, medSalary);
As a debugging tip, print out the intermediate values of variables while you're developing the code, and are not confident it is doing what you want.
So something like: cout << "sum=" << sum << endl;
will help you see what's happening to your variables, save you hours of work, and help you learn quicker. When everything works, take those lines out, or comment them out.