Getting averages from data from txt file

I have been given a text file that contains 365 lines, 1 number on each line. Each number is the amount of steps someone took per day for a whole year. The task is to calculate the average number of steps per month, using the daily amount of steps. I am completely stumped. I have no idea how I would take chunks of information from the file (lines 1-31 for january, lines 32-62 for february, and so on). The for loop is still a little confusing to me. Someone else that created this program said they used a while loop with a nested for loop, but I cannot picture how that would work out. This is literally all I have so far.
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
  This program displays the average number of steps taken per month,
based on information from a text file. */

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

int main() 
{
	ifstream inputFile;

	// Amount of days for each month
	
	/* Jan, Mar, May, July, Aug, Oct, Dec = 31 days
	   Apr, June, Sept, Nov = 30 days
	   Feb = 28 days */
	
	int avg, days, value;
	
	
	// Open the file
	inputFile.open("steps.txt");
	

	
	
	
	
	
	
	
	

	
	return 0;
	
}
Last edited on
I have no idea how I would take chunks of information from the file

http://www.cplusplus.com/doc/tutorial/files/
Last edited on
There's no need to look too far ahead. Build up the code in stages.

First read a single value from the file. Write the code, test it.

When you have that working, try to read 31 values from the file. Calculate their average. Write the code, test it.

When you have that working, start to consider how you might enclose what you have so far within an outer loop.

Topic archived. No new replies allowed.