summaryrefslogtreecommitdiffstats
path: root/C++/stepsCount/stepCount.cpp
blob: 0a89f9860879addc3f705f2221c899e99cb74ccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Name: msglm
// Date: Feb-14-2021
// Program Name: Step Count
// Description: Takes in the amount of steps you took through a week and averages them together


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

// Named constants

int main() {

	//Variable declaration
	ifstream inputData;
	ofstream stepsFormatted;
	string tempDay = "-1";
	int tempSteps = -1;
	int epoch;
	int averageSteps;
	int totalSteps;
	vector<int> stepPendingAverage; //Vector was chosen for its ease of being able to append

	//Program title and description for the user
	cout << "Title: Step Count" << endl << "Description: Takes in the amount of steps you took through a week and averages them together" << endl;

	// User input
	// Dealing with input/output files
	inputData.open("inputData.txt");
	stepsFormatted.open("stepsFormated.txt");

	//Create and set up file's formatting + columns
	stepsFormatted << fixed << showpoint << setprecision(2);
	stepsFormatted << setw(17) << left << "Day " << setw(8) << "Steps" << endl;
	//Reading from the file

	//This will loop over so long as the file is not empty
	while( !inputData.eof() ) { 

		//Test to see if tempDay and tempSteps is assigned to valid input
		if (tempDay != "-1" && tempSteps != -1) {
			//if tempDay and tempSteps is valid, then write to the file what is desired
			stepsFormatted << setw(17) << left << tempDay << setw(8) << tempSteps << endl;
			//invalidate the input so it may be wrote over again
			tempDay = -1;
			tempSteps = -1;
			//TODO find a better way to do this; there may be a time where -1 is valid input.
		}

		//If the input is a day, write the day name to tempDay
		if (epoch % 2 == 0) {
			inputData >> tempDay; 
		} else { 
			//Otherwise, is a number of steps and write that number to tempSteps
			inputData >> tempSteps;
			//also append number of steps to the temporary pending steps array
			stepPendingAverage.push_back(tempSteps);

		}
		//Increase the epoch by 1, showing that a pair of day and steps has been read
		epoch++;
	}

	// Calculations

	// Obtaining total number of steps
	for(int iteration=0; iteration < stepPendingAverage.size(); iteration++) {
		totalSteps = totalSteps + stepPendingAverage[iteration];
		//Obtain Average
		averageSteps = totalSteps/stepPendingAverage.size();
	}
	//Writing average to file
	stepsFormatted << setw(17) << left << "Average" << setw(8) << averageSteps << "steps per day" << endl;

	//Close files
	inputData.close();
	stepsFormatted.close();
	
	// Output to the screen
	cout << "Please Check stepsFormatted.txt file" << endl;

	return 0;
}

/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */