/* CST 116 LAB3 completed by Bold D. */ #include #include // For the files #include // For manipulators & formatting options #include using std::cin; using std::cout; using std::endl; using std::setw; using std::ios; using std::ifstream; using std::ofstream; int total_passengers = 0; // I set it as a global variable float grand_total_fare = 0; // global variable float Average_CPP = 0; // Average cost per person is sat a global variable const int MAX = 500; int ReadData(ifstream& inFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[]); void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], int counter); void PrintTotalsAndSummary(ofstream& out, int totalRecords); int main() { int pick[MAX]; int drop[MAX]; int psgr[MAX]; float dist[MAX]; float fare[MAX]; float toll[MAX]; float total[MAX]{}; int record_counter(0); std::string file_name; cout << " \t\tEnter one of the following: small.txt or large.txt: " << "\n\n" << "\t\t\t\t"; cin >> file_name; cout << "\n"; ifstream inFile; ofstream outFile("lab3_Report.txt"); // Notice how this automatically opens the file inFile.open(file_name); // inFile.open() function opens file_name which was created before. if (inFile.is_open()) //.is_open() checks if the file is open { record_counter = ReadData(inFile, pick, drop, psgr, dist, fare, toll); //ReadData() function called and it returns counter inFile.close(); //If the file opened, it should be clsoed back. if (outFile.is_open()) //It checks if lab3_Report.txt file is open or not { WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, record_counter); //WriteOutputFile function is called PrintTotalsAndSummary(outFile, record_counter); //PrintTotalsAndSummary function is called outFile.close(); //It needs to be closed } 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 psgr[], float dist[], float fare[], float toll[]) { int counter = 0; inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; // Priming Read float total_fare[MAX]; //Here, I initialized the total_fare[MAX] float cost_per_mile[MAX]; //Here, I initialized the cost per mile as well while (!inFile.eof()) //While inFile or file_name is open { total_fare[counter] = fare[counter] + toll[counter]; //Here, total_fare[] is calculated. cost_per_mile[counter] = fare[counter] / dist[counter]; //Here, cost per mile is calculated. cout << setiosflags(ios::left) //It prints out the following arrays on the screen << setw(5) << pick[counter] << resetiosflags(ios::left) << setw(10) << drop[counter] << resetiosflags(ios::left) << setw(12) << psgr[counter] << resetiosflags(ios::left) << setw(14) << dist[counter] << resetiosflags(ios::left) << setw(14) << fare[counter] << resetiosflags(ios::left) << setw(14) << toll[counter] << resetiosflags(ios::left) << setw(14) << total_fare[counter] //This line calculates total fare << setw(14) << cost_per_mile[counter] // This line calculates the cost per mile. << endl; counter++; inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; //It prints the above arrays from file file_name } for (int p = 0; p < counter; p++) { total_passengers = total_passengers + psgr[p]; // This for loop adds up all passengers in psgr[]. grand_total_fare = grand_total_fare + total_fare[p]; // This loop adds up all total fare. Average_CPP = Average_CPP + cost_per_mile[p]; // This loop adds up all cost per mile to Average cost per person. } return counter; //The actual or return value of ReadData function converts to int record_counter. } void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], int counter) { outFile << " Here is the Output File\n\n" << endl; for (int r = 0; r <= counter - 1; r++) { outFile << setiosflags(ios::left) << setw(5) << pick[r] << resetiosflags(ios::left) << setw(10) << drop[r] << resetiosflags(ios::left) << setw(12) << psgr[r] << resetiosflags(ios::left) << setw(14) << dist[r] << resetiosflags(ios::left) << setw(14) << fare[r] << resetiosflags(ios::left) << setw(14) << toll[r] << resetiosflags(ios::left) << setw(14) << toll[r] + fare[r] //This line prints out the total << setw(14) << fare[r] / dist[r] //This line prints out the cost per mile << endl; } } void PrintTotalsAndSummary(ofstream& outFile, int totalRecords) { // To screen cout << "\n\n\t\t** Total Records: " << totalRecords << " **\n\n" << "\t\t Total people transforted: " << total_passengers << "\n" << "\t\t\t Total paid: $" << grand_total_fare << "\n" << "\t\t Average cost per person: $" << Average_CPP << "\n"; // To file outFile << "\n\n\t** Total Records: " << totalRecords << " **\n\n" << "\t\t Total people transforted: " << total_passengers << "\n" << "\t\t\t Total paid: $" << grand_total_fare << "\n" << "\t\t Average cost per person: $" << Average_CPP << "\n"; }