Pass Values from Function to Main()

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.

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int OCCUPATION_MAX = 10;

void sort(string occupation[], double salary[]);

double calc(double avgSalary, double medSalary);

int main()
{
  char inputFile[30];	
  char outputFile[] = "output.txt";
  string occupation[OCCUPATION_MAX];	//array of type string for the occupation names
  double salary[OCCUPATION_MAX];
 
  int index = 0;
  double sum = 0;

  double avgSalary;
  double medSalary;

  //Prompt for name of input file
  cout << "Enter name of input file: ";
  cin >> inputFile;

  //Open input file
  ifstream fin;
  fin.open(inputFile);

  //If input file doesn't exist
  if  (!fin)
  {
    //Show error message
    cout << "Input file does not exists." << endl;
    return 1;
  }

  //If input file exists, read all records from file
  while (fin >> occupation[index])
  {
    fin >> salary[index];
    sum = sum + salary[index];
    index++;
  }
  //Close input file
  fin.close();
	
  //Call function to sort arrays in descending order by salary
  sort(occupation, salary);

	
  //Call function to calculate average and median of salaries
  calc(avgSalary, medSalary);
	
  //Open output file
  ofstream fout;
  fout.open(outputFile);
	
  //Write file to output
  fout << setw(15) << "OCCUPATION" << setw(15)<< "SALARY" << endl;
  fout << "------------------------------------" << endl;
  for (index = 0; index<OCCUPATION_MAX; index++)
  {
    fout << setw(15) << occupation[index] << setw(10) <<setprecision(2) << fixed << "$ " << salary[index] << endl;
  }
  fout << endl << setw(15) << "Average Salary" << setw(10) <<setprecision(2) << fixed << "$ " << avgSalary << endl;
  fout << setw(15) << "Median Salary" << setw(10) <<setprecision(2) << fixed << "$ " << medSalary << endl;
	
  //Close output file
  fout.close();

  return 0;
}

//Function to sort both arrays in descending order by salary
void sort(string occupation[], double salary[])
{
  for (int i=0; i<OCCUPATION_MAX; i++)
  {
    for (int j=i+1; j<OCCUPATION_MAX; j++)
    {
      if (salary[i] < salary[j])
      {
        //Swap salary
        double temp = salary[i];
        salary[i] = salary[j];
        salary[j] = temp;

        //Swap occupations
        string t = occupation[i];
        occupation[i] = occupation[j];
        occupation[j] = t;
      }
    }
  }
}

//Function to calculate average and median
double calc(double avgSalary, double medSalary)
{
  double salary[OCCUPATION_MAX];
  double sum = 0;

  //Calculate average of occupations
  avgSalary = sum / OCCUPATION_MAX;

  //Calculate median of occupations
  medSalary = (salary[OCCUPATION_MAX/2] + salary[(OCCUPATION_MAX-1)/2])/2;
}
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.

http://www.cplusplus.com/forum/beginner/7287/#msg33628
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.