// Trenton Stark // CST 116 // Lab 3 #include #include #include #include using std::cout; using std::cin; using std::endl; using std::ifstream; using std::string; using std::setw; using std::setprecision; using std::fixed; const int width = 10; //Affects all tables void readFile(ifstream& fin, double fares[], int& totalPass); void summary(double fares[], int& totalPass); int main() { ifstream fin; string fName; double fares[50] = { 0 }; //Initializes all cells to 0 int totalPass = 0; //Loops prompt until user enters openable file do { cout << "Input file name: "; cin >> fName; fin.open(fName); if (!(fin.is_open())) { cout << "File couldn't be opened. Try again." << endl; } } while (!(fin.is_open())); cout << endl; cout << setw(width) << "Pick:" << setw(width) << "Drop:" << setw(width) << "Pass #:" << setw(width) << "Dist:" << setw(width) << "Fare:" << setw(width) << "Toll:" << setw(width) << "Total:" << setw(width) << "Cost/Mi:" << endl; readFile(fin, fares, totalPass); //Inputs data into fares and totalPass while printing out file contents summary(fares, totalPass); //Uses fares and totalPass to create a summary } void readFile(ifstream& fin, double fares[], int& totalPass) { int i = 0; int pick, drop, pass; double dist, fare, toll, total, cosMi; cout << fixed << setprecision(2); //Formats the output down to two decimal points while (!(fin.eof())) { fin >> pick >> drop >> pass >> dist >> fare >> toll; total = fare + toll; if (dist == 0) { //Checks if there will be a division by zero, if so set cosMi to 0 cosMi = 0; } else { cosMi = fare / dist; } cout << setw(width) << pick << setw(width) << drop << setw(width) << pass << setw(width) << dist << setw(width) << fare << setw(width) << toll << setw(width) << total << setw(width) << cosMi << endl; totalPass += pass; fares[i] = total; i++; } cout << endl; } void summary(double fares[], int& totalPass) { float totalPaid = 0, average; //Totals paid must start as zero because each cell is added to the total int i; for (i = 0; i < 50; i++) { //Adds all cells regardless of how many were written because unwritten cells will be 0 totalPaid += fares[i]; } average = totalPaid / totalPass; cout << "Total Passangers: " << totalPass << endl; cout << "Total Fares: " << totalPaid << endl; cout << "Mean fare per passanger: " << average << endl; }