#include #include // For the files!!!! #include // For manipulators & formatting options #include #include 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 namespace std; 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); int main() { 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 }; int record_counter(0); int people(0); int counter = 0; float paid = 0; 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; inFile.open(filename); if (inFile.is_open()) { record_counter = ReadData(inFile, outFile, pick, drop, passCount, distance, fare, toll, costPMile, counter, totalFare); inFile.close(); if (outFile.is_open()) { WriteOutputFile(outFile, record_counter, people, pick, drop, passCount, distance, fare, toll, costPMile); 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, ofstream& outFile, int pick[], int drop[], int passCount[], float distance[], float fare[], float toll[], float costPMile[], int counter, float totalFare[]) { 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 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[counter] == 0) { costPMile[counter] = 0; } 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[], float distance[], float fare[], float toll[], float costPMile[]) { 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[], float distance[], float fare[], float toll[], float totalFare[], float costPMile[], float paid) { for (int i = 0; i < totalRecords; i++) { people += passCount[i]; float 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 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 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"; }