//Lab 3 Nathan Turcas #include #include #include // for files #include #include using namespace std; using std::cin; using std::cout; using std::endl; using std::setw; using std::ios; using std::string; using std::ifstream; using std::ofstream; const int MAX = 50; int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgrCount[], float distance[], float fare[], float toll[], float cpm[], int counter, float totalFare[]); void WriteOutputFile(ofstream& outFile, int totalRecords, int psgr, int pick[], int drop[], int psgrCount[], float distance[], float fare[], float toll[], float cpm[]); void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int psgr, int psgrCount[], float distance[], float fare[], float toll[], float totalFare[], float cpm[], float paid); int main() { int pick[MAX]; int drop[MAX]; int psgrCount[MAX]; float distance[MAX] = { 0 }; float fare[MAX]; float toll[MAX]; float totalFare[MAX] = { 0 }; float cpm[MAX] = { 0 }; int record_counter(0); int psgr(0); int counter = 0; float paid = 0; string file; ifstream inFile; // write ofstream outFile("output.txt"); //ouputs to this document cout << "Enter file name including extensions: " << endl; //use big.txt cin >> file; inFile.open(file); if (inFile.is_open()) { record_counter = ReadData(inFile, outFile, pick, drop, psgrCount, distance, fare, toll, cpm, counter, totalFare); inFile.close(); if (outFile.is_open()) { WriteOutputFile(outFile, record_counter, psgr, pick, drop, psgrCount, distance, fare, toll, cpm); PrintTotalsAndSummary(outFile, record_counter, psgr, psgrCount, distance, fare, toll, totalFare, cpm, paid); outFile.close(); } else { cout << "Trouble Opening File"; cout << "\n\n\t\t ** About to EXIT NOW! ** " << endl; } } else { cout << "Trouble Opening File"; cout << "\n\n\t\t ** About to EXIT NOW! ** " << endl; } return 0; } int ReadData(ifstream& inFile, ofstream& outFile, int pick[], int drop[], int psgrCount[], float distance[], float fare[], float toll[], float cpm[], int counter, float totalFare[]) { //heading for table cout << setiosflags(ios::left) << setw(10) << "Trip" << setw(15) << "PickUp" << setw(10) << "DropOff" << setw(10) << "# people" << setw(10) << "Distance" << setw(10) << "Fare" << setw(10) << "Toll" << setw(10) << "Total" << setw(10) << "Cost per mile" << endl; inFile >> pick[counter] >> drop[counter] >> psgrCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; //priming reading while (!inFile.eof()) { totalFare[counter] = fare[counter] + toll[counter]; //calculate total fare cout << setiosflags(ios::left) << setw(10) << counter << setw(15) << pick[counter] << setw(10) << drop[counter] << setw(10) << psgrCount[counter] << setw(10) << distance[counter] << setw(10) << fare[counter] << setw(10) << toll[counter] << setw(15) << totalFare[counter] << setw(10) << endl; if (distance[counter] == 0) { cpm[counter] = 0; } else { //calculates cost per mile cpm[counter] = (toll[counter] + fare[counter]) / distance[counter]; } cout << setprecision(2) << cpm[counter] << endl; counter++; inFile >> pick[counter] >> drop[counter] >> psgrCount[counter] >> distance[counter] >> fare[counter] >> toll[counter]; } return counter; } void WriteOutputFile(ofstream& outFile, int totalRecords, int psgr, int pick[], int drop[], int psgrCount[], float distance[], float fare[], float toll[], float cpm[]) { 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) << psgrCount[r] << setw(15) << distance[r] << setw(15) << fare[r] << setw(15) << toll[r] << setw(15) << cpm[r] << endl; } } void PrintTotalsAndSummary(ofstream& outFile, int totalrecords, int psgr, int passCount[], float distance[], float fare[], float toll[], float totalFare[], float cpm[], float paid) { for (int i = 0; i < totalrecords; i++) { psgr += passCount[i]; float num = fare[i] + toll[i]; paid += num; totalFare[i] = num; if (distance == 0) { cpm[i] = 0; } else { cpm[i] = totalFare[i] / distance[i]; cout << cpm[i] << endl; } } //to screen cout << "\n\n\tAverage Cost Per Person: " << paid / psgr << "\n" << "\n\n\t Number of People Transported: " << psgr << "\n" << "\n\n\tTotal Cost: " << paid << "\n" << "\t\t The End \n" << endl; //to file cout << "\n\n\tAverage Cost Per Person: " << paid / psgr << "\n" << "\n\n\t Number of People Transported: " << psgr << "\n" << "\n\n\tTotal Cost: " << paid << "\n" << "\t\t The End \n" << endl; }