summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortafaar <[email protected]>2022-11-28 20:54:17 -0800
committertafaar <[email protected]>2022-11-28 20:54:17 -0800
commit04d3796b2b109f21a9304fcf89301979b0769495 (patch)
treef79c5b93f60b131f5d0b5d3ee37e2226baedaf2e
parenttable formatting (diff)
downloadcst116-lab3-hill-04d3796b2b109f21a9304fcf89301979b0769495.tar.xz
cst116-lab3-hill-04d3796b2b109f21a9304fcf89301979b0769495.zip
added comments
-rw-r--r--BlankConsoleLab/BlankConsoleLab.cpp30
1 files changed, 27 insertions, 3 deletions
diff --git a/BlankConsoleLab/BlankConsoleLab.cpp b/BlankConsoleLab/BlankConsoleLab.cpp
index 0db9226..94a46c2 100644
--- a/BlankConsoleLab/BlankConsoleLab.cpp
+++ b/BlankConsoleLab/BlankConsoleLab.cpp
@@ -23,6 +23,9 @@ int numEntries;
int ReadData(ifstream& file) {
+ // Takes a file input, reads the arrays in the file, and puts them in the corresponding array in the program.
+ // t is the number of entries found. Starts at -1 because the loop begins with an increment.
+
int t = -1;
while (!file.eof()) {
@@ -31,6 +34,8 @@ int ReadData(ifstream& file) {
file >> pickUp[t] >> dropOff[t] >> passengerCount[t] >> distanceTravelled[t] >> fareAmount[t] >> tollAmount[t];
+ // Below are the calculations for the total fare and cost per mile.
+
totalFare[t] = fareAmount[t] + tollAmount[t];
if (distanceTravelled[t] != 0) {
@@ -38,11 +43,11 @@ int ReadData(ifstream& file) {
costPerMile[t] = fareAmount[t] / distanceTravelled[t];
}
- else costPerMile[t] = 0;
+ else costPerMile[t] = 0; // Special case for 0 miles ; prevents divide by 0 error.
}
- t++;
+ t++; // Increment outside of the loop. Arrays begin at 0, but the returning value is the total number of objects, hence +1 at the end.
return t;
@@ -50,6 +55,9 @@ int ReadData(ifstream& file) {
void GenerateTotals(int numEntries) {
+ // Loops through the arrays times the amount of entries found in the file. (This value is passed in.)
+ // Two local variables are initialized at 0 and each value from the arrays is added to the appropriate variable.
+
int totalPassengers = 0;
double totalPaid = 0;
@@ -60,6 +68,8 @@ void GenerateTotals(int numEntries) {
}
+ // Just printing the basic statistics.
+
cout << "TOTAL PASSENGERS: " << totalPassengers << endl;
cout << "TOTAL PAID: $" << totalPaid << endl;
cout << "AVG COST PER PERSON: $" << totalPaid / totalPassengers << endl;
@@ -69,6 +79,7 @@ void GenerateTotals(int numEntries) {
int main()
{
+ // Some initialization
ifstream inFile;
string fileName;
@@ -76,8 +87,12 @@ int main()
cout << fixed << setprecision(2);
char choice = 'Y';
+ // Body contained within a loop to allow the user to enter another file.
+
while (choice == 'Y') {
+ // Until the user enters a valid file name, the program will not continue.
+
while (!inFile.is_open()) {
cout << "Please enter your data file name with the .txt extension:" << endl;
@@ -96,10 +111,16 @@ int main()
}
+ // Fetching the # of entries and setting the values used in the program through ReadData().
+
int numEntries = ReadData(inFile);
+ // Printing basic statistics
+
GenerateTotals(numEntries);
+ // Asking the user if they want to show a table of the file they read in
+
choice = 'A';
while (choice != 'Y' && choice != 'N') {
@@ -128,6 +149,8 @@ int main()
}
}
+ // Finally, asking the user if they want to open another file.
+
choice = 'A';
while (choice != 'Y' && choice != 'N') {
@@ -137,8 +160,9 @@ int main()
}
- inFile.close();
+ // Stops reading the file and preps for the next potential loop.
+ inFile.close();
cout << endl;
}