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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// Global constants
const string EMP_NAME = "employee";
const string FAM_NAME = "family";
const char EMPLOYEE = 'O';
const char FAMILY = 'F';
const int MAX_ARRAY = 4;
const string IN_MED_FILE = "medical.txt";
const string OUT_MED_FILE = "medData.txt";
// prototypes
void readMedicalFile (ifstream&, double [], double [], int&, int&);
double averageExpense (double [], int&);
double highestExpense (double [], int&);
double aboveAverageExpense (double, double [], int&);
void displayExpenses (double, double, double, string, int&);
//*************************************************************************
// FUNCTION: main
//*************************************************************************
int main()
{
//VARIABLES
int cnt = 0; // counter used for employee coverage
int cnt1 = 0; // second counter used for family coverage
double empAverage; // return value from the called function averageExpense for employee average
double famAverage; // return value from the called function averageExpense for family average
double empHighest; // return value from the called function highestExpense for employee average
double famHighest; // return value from the called function highestExpense for family average
double empAbove; // return value from the called function aboveAverageExpense for employee average
double famAbove; // return value from the called function aboveAverageExpense for family average
// opening the text file medical.txt
ifstream inMedicalData;
inMedicalData.open(IN_MED_FILE.c_str());
// array's
double emp [MAX_ARRAY];
double fam [MAX_ARRAY];
// verification that file exists
if (!inMedicalData)
{
cout << "The input data file does not exist!" << endl;
cout << "Please verify input file and run the program again!" << endl;
return 5;
}
// calling different functions to compute and display the results
else
{
cout << "File opened successfully! Reading file." << endl << endl;
readMedicalFile (inMedicalData, emp, fam, cnt, cnt1);
// functions to compute the average expenses
empAverage = averageExpense (emp, cnt);
famAverage = averageExpense (fam, cnt1);
//functions to compute the highest expenses
empHighest = highestExpense (emp, cnt);
famHighest = highestExpense (fam, cnt1);
//functions to compute the above average number
empAbove = aboveAverageExpense(empAverage, emp, cnt);
famAbove = aboveAverageExpense(famAverage, fam, cnt1);
//functions to display the results
displayExpenses (empAverage, empHighest, empAbove, EMP_NAME, cnt);
cout << endl;
displayExpenses (famAverage, famHighest, famAbove, FAM_NAME, cnt1);
}
return 0;
}
//*************************************************************************
// FUNCTION: readMedicalFile
//*************************************************************************
void readMedicalFile (ifstream& inMedicalData, double emp [], double fam [], int& cnt, int& cnt1)
{
char medStatus; // temporarily store the first character in the file
bool arrayFull = false; // true if there is still space in the array
while (inMedicalData >> medStatus)
{
if (medStatus == 'O' && !arrayFull) //if first stored character is 'O' and array isn't full
{
if(inMedicalData >> emp[cnt])
{
cnt++;
if (cnt > MAX_ARRAY) //testing if array is full
{
arrayFull = true; // set to exit loop
cout << "Error -- too much data in file. Only first "
<< MAX_ARRAY << " employees will be used" << endl;
}
}
}
else if (medStatus == 'F' && !arrayFull)
{
inMedicalData >> fam[cnt1];
cnt1++;
if (cnt1 > MAX_ARRAY) //testing if array is full
{
arrayFull = true; // set to exit loop
cout << "Error -- too much data in file. Only first "
<< MAX_ARRAY << " families will be used" << endl;
}
}
}
inMedicalData.close();
return;
}
//*************************************************************************
// FUNCTION: averageExpense
// DESCRIPTION: Computes the average expenses for employee/family coverage
// and returns the the value
//*************************************************************************
double averageExpense (double avg [], int& count)
{
double total = 0;
double average;
int num = 0;
for (num = 0; num < count; num++)
total += avg[num];
average = total/count;
return average;
}
//*************************************************************************
// FUNCTION: highestCharge
// DESCRIPTION: Computes the highest expenses for employee/family coverage
// and returns the the value
//*************************************************************************
double highestExpense (double avg [], int& count)
{
double highest = 0;
int cell;
for (cell = 0; cell <= count; cell++)
{
if (avg [cell] > highest)
highest = avg [cell];
}
return highest;
}
//*************************************************************************
// FUNCTION: aboveAverageCharge
// DESCRIPTION: Computes value of a base number raised to an exponent
//*************************************************************************
double aboveAverageExpense (double average, double above [], int& count)
{
double greater = 0;
int cell;
int num = 0;
for (cell = 0; cell <= count; cell++)
{
if (above [cell] > average)
{
greater = above [cell];
num++;
}
}
return num;
}
//*************************************************************************
// FUNCTION: displayExpenses
// DESCRIPTION: Displays the results for the average, highest, and
// higher than average expenses for both
//*************************************************************************
void displayExpenses (double average, double highest, double above, string classType, int& count)
{
cout << fixed << showpoint << setprecision(2);
cout << "For " << count << " employees with " << classType << " coverage:" << endl;
cout << setw(31) << "Average expenses were $ " << average << endl;
cout << setw(31) << "Highest expenses were $ " << highest << endl;
cout << fixed << noshowpoint << setprecision(0);
cout << setw(7) << above << " " << classType << "(s)" << " expenses were above the average" << endl;
return;
}
|