diff options
| author | Musa Ahmed <[email protected]> | 2022-11-30 17:48:53 -0800 |
|---|---|---|
| committer | Musa Ahmed <[email protected]> | 2022-11-30 17:48:53 -0800 |
| commit | aaea2967bc68003cb052c564e2009379d5481bcc (patch) | |
| tree | 03e7d69f170938bf0e8264b4bf6b144fe82b259e /CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp | |
| parent | added pseudocode and output files (diff) | |
| download | cst116-lab3-m005a-aaea2967bc68003cb052c564e2009379d5481bcc.tar.xz cst116-lab3-m005a-aaea2967bc68003cb052c564e2009379d5481bcc.zip | |
renamed the files
Diffstat (limited to 'CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp')
| -rw-r--r-- | CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp b/CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp new file mode 100644 index 0000000..72076d7 --- /dev/null +++ b/CST116-Lab3-Ahmed/CST116-Lab3-Ahmed.cpp @@ -0,0 +1,170 @@ + +#include <iostream> +#include <fstream> // For the files!!!! +#include <iomanip> // For manipulators & formatting options +#include <string> +#include<array> + +using std::cin; +using std::cout; +using std::endl; +using std::setw; +using std::ios; +using std::string; +using std::ifstream; +using std::ofstream; +using std::setprecision; + + + +// The max amount of records in one file +const int MAX = 50; + +// 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]; + 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; + double paid = 0; + + // initialize files + string filename; + ifstream inFile; + 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()) + { + // 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(); + } + else + { + cout << "Trouble Opening File"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + } + else + { + cout << "Trouble Opening File"; + cout << "\n\n\t\t ** About to EXIT NOW! ** "; + } + return 0; +} +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" + << setw(10) << "Distance" << setw(10) + << "Fare" + << setw(10) << "Toll" + << setw(10) << "Total" + << 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]; + cout << setiosflags(ios::left) << setw(10) << counter << setw(15) + << pick[counter] + << setw(10) << drop[counter] << setw(10) + << passCount[counter] + << setw(10) << distance[counter] << setw(10) + << 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]; + } + cout << setprecision(3) << costPMile[counter]; + counter++; + inFile >> pick[counter] >> drop[counter] >> passCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; + cout << endl; + + } + + return counter; +} +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++) + { + + outFile << setiosflags(ios::left) << setw(15) + << pick[r] + << setw(15) << drop[r] << setw(15) + << passCount[r] + << setw(15) << distance[r] << setw(15) + << fare[r] + << setw(15) << toll[r] + << setw(15) << costPMile[r] << endl; + } + +} +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]; + double temp = fare[i] + toll[i]; + paid += temp; + totalFare[i] = temp; + + } + // 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"; + // 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" + << "\t\t The End \n"; +} + |