Get average from input in a for-loop

I'm making a program that is supposed to find the average max. and min. temperature, and average rainfall within a month, but also the total rainfall. I've made a for-loop which I'm pretty proud of, only i have no idea if i can get hold of all the inputs the user gives in this loop.

How can i store or get hold of all the inputs so that i can use them to find the average and total?

Also, feel free to give feedback on the existing code I've written.

PS: Output is written in Norwegian, they're just asking for the appropriate input

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
  #include <iostream>

using namespace std;

int main()
{
	int month;
	float minTemp;
	float maxTemp;
	float mmRainfall;
	
	cout << "Vennligst skriv inn antall dager i måneden (28-31) ";
	
	while (true)
	{
		cin >> month;
		if ((month > 31) || (month < 28))
			cout << "Vennligst skriv inn antall dager i måneden (28, 29, 30 eller 31)  ";

		else
			break;
	}


	for (int i = 0; i < month; ++i)
	{
		cout << "Dag " << i+1 << ": " << endl;

		cout << "Vennligst skriv inn den laveste tepraturen: ";
		cin >> minTemp;

		cout << "Vennligst skriv inn høyeste tempraturen: ";
		cin >> maxTemp;

		cout << "Vennligst skriv inn mengde nedbør i mm: ";
		cin >> mmRainfall;
	}
	

	
	system("pause");

	return 0;

}
Last edited on
closed account (iw7Gy60M)
You could create three new variables to keep running totals for the inputs. Every time you input minTemp in the 'for' loop you would add it to minTempTotal. Then after the 'for' loop you can use minTempTotal to calculate the average. The same would apply for the other two inputs.
Topic archived. No new replies allowed.