// Lab3 // // CST116 // // Taylor Rogers // #include #include #include using std::cout; using std::cin; using std::endl; using std::setw; using std::ios; using std::string; using std::ifstream; using std::ofstream; const int MAX = 50; int psgrsum = 0; float totalsum = 0; 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[], float total[], float cmp[], int counter); void PrintTotalsAndSummary(ofstream& out, int totalRecords, int totalPassengers, float totalSum); int main() { // MAX set to 50 since big.txt is <50 int pick[MAX]; int drop[MAX]; int psgr[MAX]; float dist[MAX]; float fare[MAX]; float toll[MAX]; float total[MAX]; float cpm[MAX]; int record_counter(0); string fnameinput; // Filename string from user input cout << "Enter the file name you wish to read data from: "; cin >> fnameinput; cout << endl; ifstream inFile; ofstream outFile("CST116-Lab3_Report-Rogers.txt"); inFile.open(fnameinput); if (inFile.is_open()) { record_counter = ReadData(inFile, pick, drop, psgr, dist, fare, toll); inFile.close(); if (outFile.is_open()) { WriteOutputFile(outFile, pick, drop, psgr, dist, fare, toll, total, cpm, record_counter); PrintTotalsAndSummary(outFile, record_counter, psgrsum, totalsum); outFile.close(); } else { cout << "Trouble Opening File" << endl; cout << "Exiting..." << endl; } } else { cout << "Trouble Opening File" << endl; cout << "Exiting..." << endl; } return 0; } // Reads data from the user specified file 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 // Column headers cout << "*****Data Read From File*****" << endl; cout << endl; cout << std::left << setw(15) << "Pickup" << setw(15) << "Dropoff" << setw(15) << "Passengers" << setw(15) << "Distance" << setw(15) << "Fare" << setw(15) << "Toll" << endl; while (!inFile.eof()) { cout << setiosflags(ios::left) << setw(15) << pick[counter] << setw(15) << drop[counter] << setw(15) << psgr[counter] << setw(15) << dist[counter] << setw(15) << fare[counter] << setw(15) << toll[counter] << endl; counter++; inFile >> pick[counter] >> drop[counter] >> psgr[counter] >> dist[counter] >> fare[counter] >> toll[counter]; } return counter; } // Writes data to the output file Lab3_Report.txt. This function includes a "total" and "cost per mile" column which it calculates using the file data. The passenger and cost totals are summed. void WriteOutputFile(ofstream& outFile, int pick[], int drop[], int psgr[], float dist[], float fare[], float toll[], float total[], float cpm[], int counter) { // Column headers outFile << "Here is the Output File" << endl; outFile << endl; outFile << std::left << setw(15) << "Pickup" << setw(15) << "Dropoff" << setw(15) << "Passengers" << setw(15) << "Distance" << setw(15) << "Fare" << setw(15) << "Toll" << setw(15) << "Total" << setw(15) << "Cost per Mile" << endl; cout << endl; cout << "*****Here is the Output File*****" << endl; cout << endl; cout << std::left << setw(15) << "Pickup" << setw(15) << "Dropoff" << setw(15) << "Passengers" << setw(15) << "Distance" << setw(15) << "Fare" << setw(15) << "Toll" << setw(15) << "Total" << setw(15) << "Cost per Mile" << endl; for (int r = 0; r <= counter - 1; r++) { // New columns for total cost and cost per mile total[r] = fare[r] + toll[r]; cpm[r] = total[r] / dist[r]; // Summing psgrsum += psgr[r]; totalsum += total[r]; // Transcribing data from file and adding total and cmp columns outFile << std::left << setw(15) << pick[r] << setw(15) << drop[r] << setw(15) << psgr[r] << setw(15) << dist[r] << setw(15) << fare[r] << setw(15) << toll[r] << setw(15) << total[r] << setw(15) << cpm[r] << endl; cout << std::left << setw(15) << pick[r] << setw(15) << drop[r] << setw(15) << psgr[r] << setw(15) << dist[r] << setw(15) << fare[r] << setw(15) << toll[r] << setw(15) << total[r] << setw(15) << cpm[r] << endl; } } // This function outputs the total number of entries from the data file, along with some statistics derived from the provided data. void PrintTotalsAndSummary(ofstream& outFile, int totalRecords, int totalPassengers, float totalSum) { float avgcost = 0; avgcost = totalSum / totalPassengers; // To screen cout << endl; cout << "Total Records: " << totalRecords << endl; cout << "Total Passengers: " << totalPassengers << endl; cout << "Total Cost: " << totalSum << endl; cout << "Average Cost: " << avgcost << endl; // To file outFile << endl; outFile << "Total Records: " << totalRecords << endl; outFile << "Total Passengers: " << totalPassengers << endl; outFile << "Total Cost: " << totalSum << endl; outFile << "Average Cost: " << avgcost << endl; }