Weird Output

In this weather statistics assignment i am getting a weird number towards the end of my output. Right before "Average monthly rainfall: " I am getting a repetitive, sometimes it is a long negative number, other times it is a shorter positive number, seems to be dependent on the type of data that I enter but I do not know where it is coming from.

for example, in the output it would show as (see bold below)

1234444Average monthly rainfall is: 3.8333

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
95
#include<iostream>
#include<string>
using namespace std;

struct weather
{
	double rainfall;
	double highTemp;
	double lowTemp;
	double avgTemp;
};

int main()
{
	weather months[12];
	double total = 0, highest, lowest, avgsum;
	int highmonth, lowmonth;
	string month[12] = { "January", "February", "March", "April", "May", 
		"June", "July", "August", "September", "October", 
		"November", "December" };

	for (int i = 0; i<12; i++)
	{
		cout << "Enter the total rainfall amount for month " << month[i] << ": ";
		cin >> months[i].rainfall;
		cout << "Enter high temperature: ";
		cin >> months[i].highTemp;

		while (months[i].highTemp < -100 || months[i].highTemp > 140)
		{
			cout << "ERROR: Temperature must be in the range of " << "-100 through 140.\n";
			cout << "\tHigh Temperature: ";
			cin >> months[i].highTemp;
		}

		cout << "Enter low temperature: ";
		cin >> months[i].lowTemp;

		while (months[i].lowTemp < -100 || months[i].lowTemp > 140)
		{
			cout << "ERROR: Temperature must be in the range of "
				<< "-100 through 40.\n";
			cout << "\tLow Temperature: ";
			cin >> months[i].lowTemp;
		}
	}

	//Calculate monthly average temperature and total rainfall for the year.

	for (int i = 0; i<12; i++)
	{
		total = total + months[i].rainfall;
		months[i].avgTemp = (months[i].highTemp + months[i].lowTemp) / 2;
	}

	highest = months[0].highTemp;
	lowest = months[0].lowTemp;

	for (int i = 1; i<12; i++)
	{
		if (months[i].highTemp>highest)
		{
			highest = months[i].highTemp;
			highmonth = i;
			cout << highmonth;
		}

		if (months[i].lowTemp<lowest)
		{
			lowest = months[i].lowTemp;
			lowmonth = i;
			cout << highmonth;
		}
	}

	avgsum = 0;

	//Calculate the average.

	for (int i = 0; i<12; i++)
	{
		avgsum = avgsum + months[i].avgTemp;
	}

	//Display data.

	cout << "Average monthly rainfall: " << total / 12 << endl;
	cout << "Total monthly rainfall: " << total << endl;
	cout << "Highest rainfall in " << month[highmonth] << " is: " << highest << endl;
	cout << "Lowest rainfall in " << month[lowmonth] << " is: " << lowest << endl;
	cout << "The average of all the monthly average temperatures is: " << avgsum / 12 << endl;

	system("pause");
	return 0;
}
Last edited on


Line 65 and 72 have semi's instead of endl;

pearlyman, i changed it to the code below, but now i am getting more weird numbers. It showed in output as

-858993460 -858993460 5 5 5 Average monthly rainfall:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (months[i].highTemp>highest)
		{
			highest = months[i].highTemp;
			highmonth = i;
			cout << highmonth << endl;
		}

		if (months[i].lowTemp<lowest)
		{
			lowest = months[i].lowTemp;
			lowmonth = i;
			cout << highmonth << endl;
		}
Hi,

I compiled using the gear icon at the top right of the code, I turned on all the warnings, got this warning.

72:21: warning: 'highmonth' may be used uninitialized in this function [-Wmaybe-uninitialized]


So the lesson here is two fold:

1. Always initialise everything, preferably at declaration.

2. Set your compiler warnings to their highest level.


Also, you could do with some more functions, 40LOC is seen a maximum, some go for even less.

Avoid "magic" numbers like 12 in your code, make them const variables instead.

Don't have using namespace std; put std:: before every std thing, it's the best way. Note that all the expert coders on this site all do it that way.

Line 52 and 82 , make use of the += operator

Good Luck !!
Topic archived. No new replies allowed.