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
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// Global constants
const char EMPLOYEE = 'O';
const char FAMILY = 'F';
const int MAX_ARRAY = 1000;
const string IN_MED_FILE = "medical.txt";
const string OUT_MED_FILE = "medData.txt";
// prototypes
void display_header();
void readMedicalFile (ifstream&, double [], double []);
double averageCharge (double [], int&);
double highestCharge (double [], int&);
double aboveAverageCharge;
double displayExpenses;
//*************************************************************************
// FUNCTION: main
// DESCRIPTION: Asks user for a base number, then calls functions to read
// an exponent and compute the base raised to the exponent, and
// then displays the results. Loops, requesting numbers until
// user enters zero for base.
// OUTPUT: Return Value: 0 on success
// CALLS TO: GetPositiveNum, CalcAnswer
//*************************************************************************
int main()
{
//VARIABLES
int counter = 0;
int cell;
ifstream inMedicalData;
inMedicalData.open(IN_MED_FILE.c_str());
// array's
double emp [MAX_ARRAY];
double fam [MAX_ARRAY];
// display header
display_header();
// 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;
}
else
{
cout << "File opened successfully! Reading file." << endl;
readMedicalFile (inMedicalData, emp, fam);
// Test to see if the data is accurately placed in the arrays (not part of program, just a test)
for (int num =0; num < 6; num++)
{
cout << "Employee expenses" << "(" << num << "): " << emp[num] << endl;
cout << "Family expenses" << "(" << num << "): " << fam[num] << endl << endl;
}
}
return 0;
}
//*************************************************************************
// FUNCTION: display_header
// DESCRIPTION: displays the program header
// INPUT: n/a
// OUTPUT: n/a
//*************************************************************************
void display_header()
{
cout << endl;
cout << "######################################################################" << endl;
cout << "This program will determine the average and highest expenses, and the " << endl;
cout << "number of expenses higher than the average expenses, for all employees" << endl;
cout << "under each health care coverage plan and display the results." << endl;
cout << "The program will access a file called \"medical.txt \", the file will" << endl;
cout << "contain an 'O' for employee only or an 'F' for family, followed by the" << endl;
cout << "out-of-pocket expenses." << endl;
cout << "######################################################################" << endl;
cout << endl << endl;
}
//*************************************************************************
// FUNCTION: readMedicalFile
// DESCRIPTION: Reads a positive number from the user
// INPUT: Parameter: description - of value to read from user
// OUTPUT: Return value: num - validated positive number
//*************************************************************************
void readMedicalFile (ifstream& inMedicalData, double emp [], double fam [])
{
char medStatus; // temporarily store the first character in the file
// bool arrayFull = false; // true if there is still space in the array
int cnt = 0; // counter used in the if statement for "O"
int cnt1 = 0; // second counter used in the if statement for "F"
int count; // counter used for the for statement to start the loop
while (inMedicalData) // file opened successfully
{
inMedicalData >> medStatus; // read first character
for (count = 0; count < MAX_ARRAY; count++)
{
if (medStatus == 'O' && cnt < MAX_ARRAY)
{
inMedicalData >> emp[cnt];
cnt++;
}
else
{
inMedicalData >> fam[cnt1];
cnt1++;
}
// try to read data on next line
inMedicalData >> medStatus;
}
}
inMedicalData.close();
return;
}
|