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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// Global constants
const string AUTOMOBILE_NAME = "automobile";
const string TRUCK_NAME = "truck";
const string MOTORCYCLE_NAME = "motorcycle";
const char AUTOMOBILE = 'A';
const char TRUCK = 'T';
const char MOTORCYCLE = 'M';
const int MAX_ARRAY = 2;
const string IN_REG_FILE = "vehicles.txt";
const string OUT_REG_FILE = "vehicleOut.txt";
// prototypes
void readRegistrationFile (ifstream&, double [], double [], double [], int&, int&, int&);
double averageExpense (double [], int&);
double highestExpense (double [], int&);
double lowestExpense (double [], int&);
double aboveAverageExpense (double, double [], int&);
void displayExpenses (double, double, double, string, int&);
//*************************************************************************
int main()
{
//VARIABLES
int cnt = 0; // counter used for employee coverage
int cnt1 = 0; // second counter used for family coverage
int cnt2 = 0;
double automobileAverage; // return value from the called function averageExpense for automobile average
double truckAverage; // return value from the called function averageExpense for truck average
double motorcycleAverage; // return value from the called function averageExpense for motorcycle average
double automobileHighest; // return value from the called function highestExpense for automobile average
double truckHighest; // return value from the called function highestExpense for truck average
double motorcycleHighest; // return value from the called function highestExpense for motorcycle average
double automobileLowest; // return value from the called function lowestExpense for automobile average
double truckLowest; // return value from the called function lowestExpense for truck average
double motorcycleLowest; // return value from the called function lowestExpense for motorcycle average
// opening the text file medical.txt
ifstream inRegistrationData;
inRegistrationData.open(IN_REG_FILE.c_str());
// array's
double truck [MAX_ARRAY];
double automobile [MAX_ARRAY];
double motorcycle [MAX_ARRAY];
// display header
display_header();
// verification that file exists
if (!inRegistrationData)
{
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;
readRegistrationFile (inRegistrationData, automobile, truck, motorcycle, cnt, cnt1, cnt2);
// functions to compute the average expenses
automobileAverage = averageExpense (automobile, cnt);
truckAverage = averageExpense (truck, cnt1);
motorcycleAverage = averageExpense (motorcycle, cnt2);
//functions to compute the highest expenses
automobileHighest = highestExpense (automobile, cnt);
truckHighest = highestExpense (truck, cnt1);
motorcycleHighest = highestExpense (motorcycle, cnt2);
//functions to compute the lowest expenses
automobileLowest = lowestExpense (automobile, cnt);
truckLowest = lowestExpense (truck, cnt1);
motorcycleLowest = lowestExpense (motorcycle, cnt2);
//functions to display results
//if no data for employee only coverage than output message only
displayExpenses (automobileAverage, automobileHighest, automobileLowest, AUTOMOBILE_NAME, cnt);
displayExpenses (truckAverage, truckHighest, truckLowest, TRUCK_NAME, cnt1);
displayExpenses (motorcycleAverage, motorcycleHighest, motorcycleLowest, MOTORCYCLE_NAME, cnt2);
}
return 0;
}
//*************************************************************************
void readRegistrationFile (ifstream& inRegistrationData, double automobile [], double truck [], double motorcycle [], int& cnt, int& cnt1, int& cnt2)
{
char regType; // temporarily store the first character in the file
string regPlates;
bool autoArrayFull = false; // check to see if there is still space in the array automobile[]
bool truckArrayFull = false; // check to see if there is still space in the array truck[]
bool motoArrayFull = false; // check to see if there is still space in the array motorcycle[]
while (inRegistrationData >> regType)
{
if (regType == 'A' && !autoArrayFull) //if first stored character is 'A' and array isn't full
{
if (cnt >= MAX_ARRAY && !autoArrayFull) //testing if array is full
{
autoArrayFull = true; // set to exit loop
cout << "Error -- too much data in file. Only first "
<< MAX_ARRAY << " automobile registration data will be used" << endl << endl;
}
else
{
inRegistrationData >> regPlates;
inRegistrationData >> automobile[cnt];
cnt++;
}
}
else if (regType == 'T' && !truckArrayFull)
{
if (cnt1 >= MAX_ARRAY && !truckArrayFull) //testing if array is full
{
truckArrayFull = true; // set to exit loop
cout << "Error -- too much data in file. Only first "
<< MAX_ARRAY << " truck registration data will be used" << endl << endl;
}
else
{
inRegistrationData >> regPlates;
inRegistrationData >> truck[cnt1];
cnt1++;
}
}
else if (regType == 'M' && !motoArrayFull)
{
if (cnt2 >= MAX_ARRAY && !motoArrayFull) //testing if array is full
{
motoArrayFull = true; // set to exit loop
cout << "Error -- too much data in file. Only first "
<< MAX_ARRAY << " motorcycle registration data will be used" << endl << endl;
}
else
{
inRegistrationData >> regPlates;
inRegistrationData >> motorcycle[cnt2];
cnt2++;
}
}
} // end while
inRegistrationData.close();
return;
}
//*************************************************************************
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;
}
//*************************************************************************
double highestExpense (double type [], int& count)
{
double highest = 0;
int cell;
for (cell = 0; cell <= count; cell++)
{
if (type [cell] > highest)
highest = type [cell];
}
return highest;
}
//*************************************************************************
double lowestExpense (double type [], int& count)
{
double lowest;
lowest = type [0];
int cell;
for (cell = 0; cell < count; cell++)
{
if (type [cell] < lowest)
lowest = type [cell];
}
return lowest;
}
//*************************************************************************
void displayExpenses (double average, double highest, double lowest, string classType, int& count)
{
if (count == 0)
cout << "There are NO " << classType << " registrations on file." << endl << endl;
else
{
cout << fixed << showpoint << setprecision(2);
cout << "For " << count << " " << classType << " registration in the county:" << endl;
cout << setw(31) << "\tLowest registration charge = $ " << lowest << endl;
cout << setw(31) << "\tHighest registration charge = $ " << highest << endl;
cout << setw(31) << "\tAverage registration charge = $ " << average << endl;
}
return;
}
|