New to c++

Hi guys, I'm in a c++ class and i'm a little lost. I'm praying for some help here. My program works fine with this compiler ,

https://www.onlinegdb.com/online_c++_compiler

but when i run it in visual studios 2017 i'm getting this error;

Error C4700 uninitialized local variable 'total' used Project24

the online compiler allows me to use the code

total += rainfall,

but i get that error when i use that code in visual studios.

Here is my code , I'd really appreciate any help. I've been working on this for hours and cannot figure out how to get it to calculate properly otherwise. This is the assignment ;

Average Rainfall
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.
After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Input Validation: Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.


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 <process.h>

using namespace std;


int main()
{
	double years,
		total,
		totalmonths,
		averagerainfall,
		totalinches,
		rainfall;

	cout << fixed << showpoint << setprecision(2);

	cout << "How many years of rain would you like to calculate? ";
	cin >> years;

	if (years >= 1)
	{

		for (int year = 1; year <= years; year++)

		{




			{

				
				
					for (int months = 1; months <= 12; months++)

					{


						cout << "Enter rain in inches for month " << months << endl;

						cin >> rainfall;
						
						if (rainfall >= 1)
						{
							total += rainfall * 12;
						} 

						else
						{
							cout << " Please enter a valid number, the program will be restarted. ";
							system("pause");
							return 0;
						}

					}

			}

		}

		totalmonths += years * 12;
		totalinches += total / 12;
		averagerainfall += totalinches / totalmonths;
		cout << " Total number of months is " << totalmonths << endl;
		cout << " The total inches of rainfall is " << totalinches << endl;
		cout << " The average rainfall per month is " << averagerainfall << endl;



	}

	else

		cout << " Please enter a valid number.";

	system("pause");
	return 0;
}
Last edited on
You didn't set total equal to anything to start with.
double total = 0

It is normally a good idea to set your variables equal to something when declaring them.
I could kick myself in the teeth. I'm pretty sure i set totalmonths, totainches, averagerainfall and total to 0 but its working now that i set all variables to 0. I can't thank you enough.
Topic archived. No new replies allowed.