0

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++;
      }
    }
8
  • And in what line(s) of code is your problem? You have a user-defined function as it's in the code - I'm not quite sure what it is that you want. Commented Dec 13, 2013 at 20:00
  • 1
    You could start by putting curly braces around your sum function. Commented Dec 13, 2013 at 20:04
  • You're not calling calc where you say you are. You're declaring it. Commented Dec 13, 2013 at 20:09
  • In your sum function, you should pass the input stream by reference. Commented Dec 13, 2013 at 21:31
  • In your sum function you are not inputting correctly. Search StackOveflow for "c++ read from file parse". Commented Dec 13, 2013 at 21:32

1 Answer 1

1

Sorry, I couldn't deal with the mistakes in your sum function:
Avoid the old function declaration and use a modern declaration:

void sum(std::istream&        input_file,
         std::vector<double>& salary,
         double &             sum,
         unsigned int&        values_read);

Read properly:

{
  double salary_read = 0.0;
  sum = 0.0;
  while (input_file >> salary_read)
  {
      salary.push_back(salary_read);
      sum += salary_read;
      ++values_read;
  }
}

Functions should have one purpose only
Input and summing are two separate functions.
You can sum after the data is input.
Separating the functions allow you to input the data with one function and use the data for another purpose without calculating the sum each time.

Review input iterators
The input iterators will allow you to tell the compiler to generate more efficient code to read files from the input stream.

Let std::vector give you the number of elements
You don't have to tally the number of elements read in.
If you use std::vector, you an use the size method to get the number of values.

The simplified functions Yes, the functions can be written more compact using the facilities in <algorithm>, but these illustrate the fundamental concepts.

std::istream& input_salaries(std::vector<double>& salaries,
                            std::istream& input_file)
{
  double value_from_file = 0.0;
  while (input_file >> value_from_file)
  {
    salaries.push_back(value_from_file);
  }
  return input_file;  // So this function can be "chained"
}

double sum(const std::vector<double>& salaries)
{
  double sum = 0.0;
  const unsigned int number_of_salaries = salaries.size();
  for (unsigned int i = 0; i < number_of_salaries; ++i)
  {
    sum += salaries[i];
  }
  return sum;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't mean to be overly nitpicky, but sum taking a stream to an input file is just odd. I would parse the file separately and pass iterators or something to sum. Otherwise sum is really just doing too much IMO
@EdS.: In the section for simplified functions, the input has been separated from the processing. The input_salaries function is not necessary and the code could be pasted inline, but the function shows the separation from the OP's sum function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.