SOURCE FILE: bonuses.cpp



#include <iostream>
#include <fstream>
#include <assert.h>
#include <iomanip>

using namespace std ;

const int MAX_NUM_SALARIES = 50 ;
const double BONUS_PERCENTAGE = 12.5 ;

void ReadSalaries (double theSalaries[ ], int & howMany) ;

void CalculateBonuses ( 
                        const double theOldSalaries[ ], 
                        double theNewSalaries[ ], 
                        int howMany
                      ) ;

void PrintResults ( 
                     const double theOldSalaries[ ], 
                     const double theNewSalaries[ ], 
                     int howMany
                  ) ;

int main (void)
{
   double Salaries[MAX_NUM_SALARIES] ;
   double New_Salaries[MAX_NUM_SALARIES] ;
   int number_of_salaries ;

   ReadSalaries (Salaries, number_of_salaries) ;
   CalculateBonuses (Salaries, New_Salaries, number_of_salaries) ;
   PrintResults (Salaries, New_Salaries, number_of_salaries) ;

   return 0;
}

void ReadSalaries (double theSalaries[ ], int & howMany) 
{
   ifstream payFile("salaries"); 
   assert(payFile);
   
   payFile >> howMany ;
   assert(howMany <= MAX_NUM_SALARIES) ;
   int salaryNum ;
   for (salaryNum = 0; salaryNum < howMany; salaryNum++)
   {
      payFile >> theSalaries[salaryNum] ;
      // The line below may be used for checking and debugging
      // cout << theSalaries[salaryNum] << endl ;
   }
   payFile.close();   
}


void UpdateSalary(double oldSalary, double & newSalary) ;

void CalculateBonuses ( 
                        const double theOldSalaries[ ], 
                        double theNewSalaries[ ], 
                        int howMany
                      ) 
{
   int salaryNum ;
   for (salaryNum=0; salaryNum<howMany; salaryNum++)
   {
      UpdateSalary (theOldSalaries[salaryNum], theNewSalaries[salaryNum] ) ;
      
      /*
           Instead of the function call above, we could have put this code
           inline here:

           theNewSalaries[salaryNum] =
             theOldSalaries[salaryNum] * (1 + BONUS_PERCENTAGE/100 ) ;
      */

   }
}

void UpdateSalary(double oldSalary, double & newSalary) 
{
   newSalary = oldSalary * (1 + BONUS_PERCENTAGE/100 ) ;
   // The line below may be used for checking and debugging
   // cout << newSalary << endl ;
}

void PrintOneLine (int id_num, double oldSalary, double newSalary) ;

void PrintResults ( 
                     const double theOldSalaries[], 
                     const double theNewSalaries[], 
                     int howMany
                  ) 
{

  cout << endl ;

       // Print the report title
  cout << "Calculation of New Total Salaries after Applying A Bonus of " 
       << BONUS_PERCENTAGE <<    " Percent\n\n" ;

       // Print the report column headings
  cout << "Employee Number      Old Salary       New Salary\n\n"  ;

  int salaryNum ;

  for (salaryNum = 0; salaryNum < howMany; salaryNum++) 
  {
         /* Call a function to print one line of the report.  The parameters
	    are the numbers that have to be printed.  (Since array indexes
	    start at 0, but we want the employee numbers to start at 1, we
	    pass salaryNum+1 to PrintOneLine() )
         */

     PrintOneLine( salaryNum+1, 
                   theOldSalaries[salaryNum], 
                   theNewSalaries[salaryNum]
                 ) ;
  }
  cout << endl ;
}

void PrintOneLine (int id_num, double oldSalary, double newSalary) 
{
   cout.setf(ios::fixed) ;
   cout.setf(ios::showpoint) ;
   cout.precision(2) ;

        /* Output the numbers, using field widths to get
           the desired alignment in columns.
        */ 
   cout << setw(8) << id_num ;
   cout << setw(23) << oldSalary ;
   cout << setw(17) << newSalary ;
   cout << endl ;
}