Weather Statistics Program Using Structs

The program needs to accept for different pieces of data which it then stores into four different arrays. It then needs to calculate the average monthly rainfall, total rainfall for the year, highest and lowest temperatures for the year, and average of all average monthly temperatures.

I'm having trouble calculating average monthly rainfall as leaving the comment line which calls the function without slashes freezes the program. Ommitting that line executes the next line but then I receive a runtime error which says something along the lines of data1 being corrupted.

Any help is appreciated.

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

struct weatherData
{
	double totRain[11];
	double highTemp[11];
	double lowTemp[11];
	double aveTemp[11];

	double yearlyMonthlyRainfall(double weather[])
	{
		int count{ 0 };
		double sum{ 0 };
		while (count < 12)
		{
			sum += weather[count];
                        count++;
		}
		double average = sum / 12;
		return average;
	}

};

int main()
{
	cout << fixed << showpoint << setprecision(2);

	weatherData data1;
	
	for (size_t count = 0; count <= 11; count++)
	{
		cout << "Please enter total rainfall for month " << count + 1 << ":" << endl;
		cin >> data1.totRain[count];
		double hTemp;
		cout << "Please enter the highest temperature reading for month " << count + 1 << ":" << endl;
		cin >> hTemp;
		if (hTemp >= -100 && hTemp <= 140)
		{
			data1.highTemp[count] = hTemp;
		}
		else
		{
			cout << "Invalid temperature value" << endl;
			exit(0);
		}
		double lTemp;
		cout << "Please enter the lowest temperature reading for month " << count + 1 << ":" << endl;
		cin >> lTemp;
		if (lTemp >= -100 && lTemp <= 140)
		{
			data1.lowTemp[count] = lTemp;
		}
		else
		{
			cout << "Invalid temperature value" << endl;
			exit(0);
		}
		double aTemp;
		cout << "Please enter average temperature for month " << count + 1 << ":" << endl;
		cin >> aTemp;
		if (aTemp >= -100 && aTemp <= 140)
		{
			data1.aveTemp[count]=aTemp;
		}
		else
		{
			cout << "Invalid temperature value" << endl;
			exit(0);
		}
	}
	
	//cout << "Average monthly rainfall: " << data1.yearlyMonthlyRainfall(data1.totRain) << endl;

	cout << "???" << endl; 
	
}
Last edited on
Why is yearlyMonthlyRainfall() defined inside the structure?

Why all the arrays inside the structure instead of an array of the structure?

Why are you trying to call the function with a single instance of your structure when the function expects an array of double as a parameter?

Why are you using a structure when you indicate that you need to use 4 arrays?
The program needs to accept for different pieces of data which it then stores into four different arrays.
I see. Is should be starting out with something like this correct?

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
struct weatherData
{
	double totRain;
	double highTemp;
	double lowTemp;
	double aveTemp;

	/*
	double yearlyMonthlyRainfall(double weather[])
	{
		int count{ 0 };
		double sum{ 0 };
		while (count < 12)
		{
			sum += weather[count];
			count++;
		}
		double average = sum / 12;
		return average;
	}
	*/

};

int main()
{
	cout << fixed << showpoint << setprecision(2);

	weatherData data1[12];
	
	for (size_t count = 0; count <= 11; count++)
	{
		cout << "Please enter total rainfall for month " << count + 1 << ":" << endl;
		cin >> data1[count].totRain;


I'll figure out the function business.
No you should probably be starting out with something like:
1
2
3
4
5
6
7
int main()
{
    const int Array_size = 10;
    double totRain[Array_size];
    double highTemp[Array_size];
    double lowTemp[Array_size];
    double aveTemp[Array_size];


Because your instructions state:
The program needs to accept for different pieces of data which it then stores into four different arrays.


Topic archived. No new replies allowed.