Why this is happening?

So I'm trying to implement reading in a file of a board game.

Now, I know the error is something to do with the first if statement, but as far as I can see, the "index" value should be 2 and thus skipping the if statements altogether.

The next index value is 1, which should hit the if statement, but it keeps erroring out and breaking as it checks the index value, any ideas?

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
    class boardSpace
    {
    public:
    	int index, cost, rent, color;
    	string street, streetType, jail;
    };
    
    
    void readFile(boardSpace marray[])
    {
    	int i = 0;
    	ifstream inFile;
    	inFile.open("Monopoly.txt");
    	while (!inFile.eof())
    	{
    			inFile >> marray[i].index;
    			inFile >> marray[i].street;
    
    			if (marray[i].index == 1)
    			{				
    				inFile >> marray[i].streetType;
    				inFile >> marray[i].cost;
    				inFile >> marray[i].rent;
    				inFile >> marray[i].color;
    			}
    
    			if (marray[i].index == 3)
    			{
    				inFile >> marray[i].streetType;
    			}
    
    			if (marray[i].index == 7)
    			{
    				inFile >> marray[i].street;
    				inFile >> marray[i].streetType;
    				inFile >> marray[i].jail;
    			}
    
    			if (marray[i].index == 8)
    			{
    				inFile >> marray[i].street;
    			}	
    
    			i++;
    
    			if (inFile.eof())
    			{
    				break;
    			}
    	}
    		inFile.close();
    }

But keep getting the following error

1
2
        Exception thrown at 0x003BADF8 in Monopoly AI.exe: 0xC0000005:
     Access violation reading location 0x01142018.



Last edited on
how does your file look like?
on output? Blank.

It seems to be an error on the line ( Line 19 )

if (marray[i].index == 1)

almost like it cannot access the next part of the array ( in spite of me adding i++; )
Last edited on
Please, post the file "Monopoly.txt".
the "index" value should be 2 and thus skipping the if statements altogether.
It skips the if statements but it does not skip the i++. It depends on the size of marray when it goes out of bounds.

I suggest that you put the i++ inside the if branch and you may use else if.

What happens when line 16 reads past the end, i.e. an eof occurs?
how does your file look like?
...
on output? Blank.

I meant your input file, actually
Last edited on
Topic archived. No new replies allowed.