// CST 116 - Andrei Florea - Lab 3 // Take in a file input with data and display statistics about the file #include #include #include using namespace std; using std::cout; using std::cin; using std::endl; using std::setprecision; using std::fixed; void getFileName(string&); void readData(fstream&, int[], int[], int[], int[], float[], float[], float[], float[], float[]); void displayData(int[], int[], int[], int[], float[], float[], float[], float[], float[], int); int main() { // Create and initiate most variables, this is where the main function of code runs int pick_up_loc[50]; int drop_off_loc[50]; int pass_count[50]; int trip[50]; float distance[50]; float fare_amount[50]; float toll_amount[50]; float total[50]; float cost_mi[50]; string file_name; getFileName(file_name); fstream data_file; data_file.open(file_name); if (data_file.is_open()) { readData(data_file, pick_up_loc, drop_off_loc, pass_count, trip, distance, fare_amount, toll_amount, total, cost_mi); } else if (!(data_file.is_open())) { cout << "Trouble opening " << file_name << "." << endl; } } void getFileName(string& name) { // Ask user for file name, store it in name cout << "Enter the name of the file including extension: " << endl; cin >> name; } void readData(fstream& file, int pick[], int drop[], int passenger[], int trip[], float distance[], float fare[], float toll[], float total[], float cost_mi[]) { // Take in each line of data and store it in the correct slot for each array int counter = 0; file >> pick[counter] >> drop[counter] >> passenger[counter] >> distance[counter] >> fare[counter] >> toll[counter]; total[counter] = fare[counter] + toll[counter]; trip[counter] = counter + 1; if (distance[counter] == 0) { cost_mi[counter] = 0; } else cost_mi[counter] = fare[counter] / distance[counter]; while( !file.eof() ) { counter++; file >> pick[counter] >> drop[counter] >> passenger[counter] >> distance[counter] >> fare[counter] >> toll[counter]; trip[counter] = counter + 1; total[counter] = fare[counter] + toll[counter]; if (distance[counter] == 0) { cost_mi[counter] = 0; } else cost_mi[counter] = fare[counter] / distance[counter]; } file.close(); displayData(trip, pick, drop, passenger, distance, fare, toll, total, cost_mi, counter); } void displayData(int trip[], int pick[], int drop[], int passenger[], float distance[], float fare[], float toll[], float total[], float cost_mi[], int counter) { // Using the arrays, display the data int total_people = 0; float total_cost = 0; int width = 15; cout << setw(width) << "Trip #" << setw(width) << "Pick Up" << setw(width) << "Drop off" << setw(width) << "# of ppl"; cout << setw(width) << "Distance" << setw(width) << "Fare" << setw(width) << "Toll"; cout << setw(width) << "Total" << setw(width) << "Cost/Mile" << setw(width) << endl; for (int idx = 0; idx <= counter; idx++) { cout << setw(width) << trip[idx] << setw(width) << pick[idx] << setw(width) << drop[idx]; cout << setprecision(2) << fixed << setw(width) << passenger[idx] << setw(width) << distance[idx] << setw(width) << fare[idx]; cout << setw(width) << toll[idx] << setw(width) << total[idx] << setw(width) << cost_mi[idx] << endl; total_people = total_people + passenger[idx]; total_cost = total_cost + fare[idx] + toll[idx]; } cout << setw(width) << "People transported: " << total_people << endl; cout << setw(width) << "Total paid: " << total_cost << endl; cout << setw(width) << "Average cost per person: " << total_cost / total_people << endl; }