I am just about done but I am having a hard time thinking of how to add up numbers in an array from a file. I want it as a user-defined function. I have done it through predefined methods but I was wondering how to make a function that does the job.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
const int OCCUPATION_MAX = 10;
void sort(string occupation[], double salary[]);
void sum(double salary[], double sum, int index);
double calc(double avgSalary, double medSalary);
int main()
{
string occupation[OCCUPATION_MAX]; //array of type string for the occupation names
double salary[OCCUPATION_MAX];
ifstream input;
ofstream output;
int index = 0;
double sum = 0;
string fname;
double average;
double median;
//Intro
cout << "Welcome to this program.\n";
cout << "Please enter text file you want to use.\n";
cin >> fname;
//opening file of correct
input.open(fname.c_str());
if(input.fail())
{
cout << "Not a valid filename\n";
exit(0);
}
//If input file exists, read all records from file
while (input >> occupation[index])
{
input >> salary[index];
sum = sum + salary[index];
index++;
}
//Close input file
input.close();
//Call function to sort arrays in descending order by salary
sort(occupation, salary);
//Call function to calculate average and median of salaries
double calc(double sum, double salary[], double& average, double& median);
//Open output file, remember to look at output.txt for the result)
output.open("output.txt");
//Write file to output
output << setw(15) << "OCCUPATION" << setw(15)<< "SALARY" << endl;
output << "------------------------------------" << endl;
for (index = 0; index<OCCUPATION_MAX; index++)
{
output << setw(15) << occupation[index] << setw(10) <<setprecision(2) << fixed << "$ " << salary[index] << endl;
}
output << endl << setw(15) << "Average Salary" << setw(10) <<setprecision(2) << fixed << "$ " << average << endl;
output << setw(15) << "Median Salary" << setw(10) <<setprecision(2) << fixed << "$ " << median << endl;
//Close output file
output.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;
}
}
}
}
void sum(double salary[], double sum, index=0)
double salary;
double sum=0;
int index=0;
{
while(!eof)
{
output >> salary[index];
sum = sum + salary[index];
index++;
}
}
//Function to calculate average and median
double calc(double sum, double salary[], double& average, double& median)
{
//Calculate average of occupations
average = sum / salary[OCCUPATION_MAX];
//Calculate median of occupations
median = (salary[OCCUPATION_MAX/2] + salary[(OCCUPATION_MAX-1)/2])/2;
}
So far I have been told to dereference my pointers.
void sum(double salary[], double sum, index=0)
double salary;
double sum=0;
int index=0;
{
while(!eof)
{
output >> salary[index];
sum = sum + salary[index];
index++;
}
}
sumfunction.calcwhere you say you are. You're declaring it.sumfunction, you should pass the input stream by reference.sumfunction you are not inputting correctly. Search StackOveflow for "c++ read from file parse".