Idk how to describe it

Hi guys, I've been working on the program that simulates the windspeed with chance of storms and microbursts during storms. So the problem is that it stops working after I add a statement on lines 87-90, or actually when I try to interact with count2 variable inside the for loop (even if I try to cout it). Why is this happening?

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

double fRand(double wmin, double wmax,double d)
{
    double f = (double)rand() / RAND_MAX;
    return d=wmin + f * (wmax - wmin);
}


int main ()
{
	srand(time(NULL));	
	
	ofstream outfile;
	outfile.open("winds.txt");
	
	outfile<<"Time(s)\tWind Speed(m/h)"<<endl; //header
	
	double d, wmax, wmin, wspd[360]={0};
	cout<<"Input the maximum and mimimum windspeed"<<endl;
	cin>>wmax>>wmin;
	
	bool st=0, STORM=0,mb=0, MB=0;
	int count, count2;
	string r,r2;
	
	cout<<"Storms? (Y/N)"<< endl;
	cin >>r;
	if (r=="Y")
		{
		cout<<"Storms are enabled."<<endl;
		st = 1;
		cout<<"Microbursts? (Y/N)"<< endl;
		cin >>r2;
		if (r2=="Y")
			{
				cout<<"Storms are enabled. Microbursts are enabled."<<endl;
				mb=1;
			}
		else
			{
				cout<<"Storms are enabled. Microbursts are disabled"<<endl;
				mb=0;
			}
		}
	else 
		{
		cout<<"Storms are disabled."<<endl;
		st = 0;
		}
	

	for (int i; i<360; i++)
	{
		
	if (st==1 && STORM==0) //chance of a storm
		{
		int luck = (rand()%(200-1+1)+1);
		if (i==10) //starts the storm if random number hits 13
			{
				STORM = 1;
				count = 0; //count is used to control the storm
			}
		}
			
	if (STORM==1)
	{
		count++;
		if (MB==0&&mb==1)
			{
			int luck = rand()%(100-1+1)+1;
			if (i==15)
				{
					MB=1;
					count2=0;
				}
			}
	}
	
	
	if (MB==1)
		{
			count2++; //!!!REFUSES TO WORK AFTER THIS STATEMENT
		}
	
	
	if (STORM==0)
	{
		wspd[i]= fRand(wmin,wmax,d);
	}
	else if(STORM==1)
	{
		wspd[i]= fRand(wmin,wmax,d) + 10; //ads 10 to the wind value if there's a storm
	}

	
	int i1=i+1;
	outfile<<i1<<"0\t"<<wspd[i]<<endl; //writes to the file
	
	
	if (count == 60) //stops the storm after 600 seconds
	{
		STORM=0;
	}


	}
	cout <<wspd[1];
	return 0;
}

The problem is in line 59 - i is not initialized so in line 99 you access wspd with an invalid index.
The problem is in line 59 - i is not initialized so in line 99 you access wspd with an invalid index.

It is initialized and line 99 works perfectly if I remove the statement in lines 87-90.
It is initialized

Oh yes? On what line do you set its initial value?
The variable i is not initialized - it has a random value.
for (int i; i<360; i++)

In the debugger on line 99 i had the value of -858993460. Accessing wspd with this index is undefined behaviour.

Oh yes? On what line do you set its initial value?

Oops... Didn't realize that was the case.
Then why does it actually work perfectly without lines 87-90 even with i is not initialized?

Thank you for help guys.
Last edited on
Topic archived. No new replies allowed.