diff options
Diffstat (limited to 'BlankConsoleLab/Lab3-Ahmed.cpp')
| -rw-r--r-- | BlankConsoleLab/Lab3-Ahmed.cpp | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/BlankConsoleLab/Lab3-Ahmed.cpp b/BlankConsoleLab/Lab3-Ahmed.cpp index a78d875..72076d7 100644 --- a/BlankConsoleLab/Lab3-Ahmed.cpp +++ b/BlankConsoleLab/Lab3-Ahmed.cpp @@ -13,48 +13,60 @@ using std::ios; using std::string; using std::ifstream; using std::ofstream; -using namespace std; +using std::setprecision; +// The max amount of records in one file const int MAX = 50; -int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[], int counter, float totalFare[]); -void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[]); -void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], float paid); +// initialize all functions +int ReadData(ifstream& inFile, int pick[], int drop[], int passCount[], double distance[], double fare[], double toll[], double costPMile[], int counter, double totalFare[]); +void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], double distance[], double fare[], double toll[], double costPMile[]); +void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int passCount[], double distance[], double fare[], double toll[], double totalFare[], double costPMile[], double paid); + int main() { + // initialize all arrays and counters int pick[MAX]; int drop[MAX]; int passCount[MAX]; - float distance[MAX]; - float fare[MAX]; - float toll[MAX]; - float totalFare[MAX] = { 0 }; - float costPMile[MAX] = { 0 }; + double distance[MAX]; + double fare[MAX]; + double toll[MAX]; + double totalFare[MAX] = { 0 }; + double costPMile[MAX] = { 0 }; int record_counter(0); int people(0); int counter = 0; - float paid = 0; + double paid = 0; + // initialize files string filename; ifstream inFile; - - // Notice how this automatically opens the file ofstream outFile("out.txt"); + + cout << "Please enter the input file name including extension: " << endl; cin >> filename; + + // open the requested file inFile.open(filename); if (inFile.is_open()) { - record_counter = ReadData(inFile, outFile, pick, drop, passCount, distance, fare, toll, costPMile, counter, totalFare); + // read the data from the file, and output the total number of records (store into arrays) + record_counter = ReadData(inFile, pick, drop, passCount, distance, fare, toll, costPMile, counter, totalFare); + + // close the read file inFile.close(); if (outFile.is_open()) { + // write the new data to the output file WriteOutputFile(outFile, record_counter, people, pick, drop, passCount, distance, fare, toll, costPMile); + // output the summary both to screen and to file PrintTotalsAndSummary(outFile, record_counter, people, passCount, distance, fare, toll, totalFare, costPMile, paid); outFile.close(); } @@ -71,10 +83,10 @@ int main() } return 0; } -int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[], int counter, float totalFare[]) +int ReadData(ifstream& inFile, int pick[], int drop[], int passCount[], double distance[], double fare[], double toll[], double costPMile[], int counter, double totalFare[]) { - + // print headers cout << setiosflags(ios::left) << setw(10) << "Trip" << setw(15) << "Pick" << setw(10) << "Drop" << setw(10) << "# ppl" @@ -85,6 +97,7 @@ int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int pa << setw(10) << "Cost per Mile" << endl; inFile >> pick[counter] >> drop[counter] >> passCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; // Priming Read + // read all of the data into arrays while (!inFile.eof()) { totalFare[counter] = fare[counter] + toll[counter]; @@ -96,9 +109,11 @@ int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int pa << fare[counter] << setw(10) << toll[counter] << setw(15) << totalFare[counter] << setw(10); + // if distance is 0 then there is no cost per mile if (distance[counter] == 0) { costPMile[counter] = 0; } + // cost per mile = (toll + fare) / distance else { costPMile[counter] = (toll[counter] + fare[counter]) / distance[counter]; } @@ -111,10 +126,10 @@ int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int pa return counter; } -void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[]) +void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[], int drop[], int passCount[], double distance[], double fare[], double toll[], double costPMile[]) { - + // write the same data to the output files outFile << " Here is the Output File" << endl; for (int r = 0; r < totalRecords; r++) { @@ -130,31 +145,23 @@ void WriteOutputFile(ofstream& outFile, int totalRecords, int people, int pick[] } } -void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int passCount[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], float paid) +void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int people, int passCount[], double distance[], double fare[], double toll[], double totalFare[], double costPMile[], double paid) { + // do the calculations for all of the totals. for (int i = 0; i < totalRecords; i++) { people += passCount[i]; - float temp = fare[i] + toll[i]; + double temp = fare[i] + toll[i]; paid += temp; totalFare[i] = temp; - if (distance == 0) { - costPMile[i] = 0; - } - - else { - costPMile[i] = totalFare[i] / distance[i]; - // cout << costPMile[i]; - } } - // To screen + // print ot screen cout << "\n\n\t** Avg Cost Per Person: " << paid/people << " **\n" << "\n\n\t** People Transported: " << people << " **\n" << "\n\n\t** Total Cost: " << setprecision(5) << paid << " **\n" << "\t\t The End \n"; - - // To file + // print to output file outFile << "\n\n\t** Avg Cost Per Person: " << paid/people << " **\n" << "\n\n\t** People Transported: " << people << " **\n" << "\n\n\t** Total Cost: " << paid << " **\n" |