//-------------------------------------------------------------------
// Author: W. T. Door
// Date: 12 week exam
// Filename: 12weeker.cpp
// Description: A program that produces a quarterly flight hour report
//-------------------------------------------------------------------
 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
void readSquadronData(string fileName, int& numb_pilots, int& qtrlyHours, string& sqdName);
void readMontlyFlightHours(string fileName, int* flightHours);
void quarterlyHoursReport(int numbPilots, int* flightHours, string sqdName, int minHours);
 
int main()
{
        string fileName, junk, squadron;
        int numbPilots, minHours;
        int *flightHrs;
 
        fileName = "SquadronInfo.txt";
        readSquadronData(fileName, numbPilots, minHours, squadron);
 
        flightHrs = new int[numbPilots];
 
        for (int j = 0 ; j < numbPilots; j++)
               flightHrs[j] = 0;
 
        for (int i = 0; i < 3; i++)
        {
               cout << "Enter flight hour data file name: ";
               cin >> fileName;
               readMontlyFlightHours(fileName, flightHrs);
        }
 
        quarterlyHoursReport(numbPilots, flightHrs, squadron, minHours);
 
        return 0;
}
 
void readSquadronData(string fileName, int& numbPilots, int& avgQtrlyHours, string& sqd)
{
        string junk;
        ifstream file_in;
        file_in .open(fileName.c_str());
        if (!file_in)
        {
               cout << "* Error * Unable to open file (" << fileName << ")" << endl;
               return;
        }
        else 
        {
               file_in >> sqd;
               file_in >> junk >> junk >> junk >> avgQtrlyHours;
               file_in >> junk >> junk >> numbPilots;
        }
}
 
void readMontlyFlightHours(string fileName, int* flightHours)
{
        string junk;
        char c;
        int pilot, hours;
        ifstream file_in;
        file_in .open(fileName.c_str());
        if (!file_in)
        {
               cout << "* Error * Unable to open file (" << fileName << ")" << endl;
               exit(0);
        }
        else 
        {
               file_in >> junk >> junk;
               while (file_in >> c >> pilot >> hours)  // accumulate monthly flight hours
               {
                       flightHours[pilot-1] += hours;
               }
        }
}
 
void quarterlyHoursReport(int numbPilots, int* flightHours, string sqdName, int minHours)
{
        int totHrs = 0;
        int highPilotNumb, highPilotHrs = 0;
        double qtrlyAvg;
 
        for (int i = 0; i < numbPilots; i++)
        {
               totHrs += flightHours[i];
               if (flightHours[i] > highPilotHrs)  // this is a new high flight hour pilot
               {
                       highPilotHrs = flightHours[i];
                       highPilotNumb = i;
               }
        }
 
        qtrlyAvg = double(totHrs)/numbPilots;
 
        cout << endl << "** " << sqdName << " QUARTERLY FLIGHT HOUR REPORT **" << endl;
        cout << "Total flight hours for the quarter: " << totHrs << endl<< endl;
 
        cout << "High time pilot was P" << highPilotNumb+1 << " with "
                << flightHours[highPilotNumb] << " total hours for the quarter." << endl << endl;
 
        cout << "Average quarterly flight hours per pilot was: " << qtrlyAvg << endl << endl;
 
        cout << "The following pilots flew less than the desired minimum hours for the quarter:" << endl;
 
        for (int j = 0; j < numbPilots; j++) {
               if (flightHours[j] < minHours)
                       cout << 'P' << j +1 << '\t' << flightHours[j] << " hours" <<endl;
        }
        cout << endl;
 
 
}
 
 

syntax highlighted by Code2HTML, v. 0.9.1