diff options
| author | Andrei F <[email protected]> | 2022-11-30 17:23:56 -0800 |
|---|---|---|
| committer | Andrei F <[email protected]> | 2022-11-30 17:23:56 -0800 |
| commit | 7caa39baad8c2bd30e6f35296088238c82947466 (patch) | |
| tree | 0154aa0092dc7bed5dc41adcd855f34cb009554e /Lab3 | |
| parent | Adding some comments, changing file names (diff) | |
| download | cst116-lab3-florea-7caa39baad8c2bd30e6f35296088238c82947466.tar.xz cst116-lab3-florea-7caa39baad8c2bd30e6f35296088238c82947466.zip | |
Finished working project
Diffstat (limited to 'Lab3')
| -rw-r--r-- | Lab3/CST116-Lab3-Florea.cpp | 108 |
1 files changed, 95 insertions, 13 deletions
diff --git a/Lab3/CST116-Lab3-Florea.cpp b/Lab3/CST116-Lab3-Florea.cpp index b8c3fe0..d392d95 100644 --- a/Lab3/CST116-Lab3-Florea.cpp +++ b/Lab3/CST116-Lab3-Florea.cpp @@ -3,32 +3,114 @@ #include <iostream> #include <string.h> +#include <fstream> using namespace std; using std::cout; -using std::cin; +using std::cin; using std::endl; +using std::setprecision; +using std::fixed; -string getFileName(); +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() { - cout << "Hello World!\n"; - getFileName(); + 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; + string data_contents; + 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; + } + } -string getFileName() +void getFileName(string& name) { - string file_name; cout << "Enter the name of the file including extension: " << endl; - cin >> file_name; - - return file_name; + cin >> name; } -void readData() +void readData(fstream& file, int pick[], int drop[], int passenger[], int trip[], float distance[], float fare[], float toll[], float total[], float cost_mi[]) +{ + 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) { - int pick_up_loc; - int drop_off_loc; -}
\ No newline at end of file + 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; + + +} |