Need help with fixing my code

closed account (SEw0ko23)
Hi everyone, I have been making this game for a few weeks now for my uni assignment. I have finished writing the code for it, however while testing the game, i came across an error, which is where the game crashes when i would want to reply a specific level. For example, playing level 1, completing it and selecting it again will crash the game and force close it. I have tried many solutions over the past few days, such as using memset, hard-coding the starting position of X or using memcpy. I have been unsuccessful with all these methods as my coding skills are not very high, therefore I am relying on this forum to fix my only problem, as the deadline is tomorrow.
Appreciate the help

The full code has been linked here as it did not fit in the forum: https://drive.google.com/open?id=0B2lLG0MNZMxdWVBMeGd2S1RWc3c

I have been using Windows Visual Studio 2015 to compile the code, and the code linked should compile and run.
the code linked should compile and run.

Actually it should fail to compile:
main.cpp||In function ‘int main()’:|
main.cpp|352|error: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]|
main.cpp|368|error: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]|


You should also avoid using goto, learn to use some of the other more accepted loop constructs instead.

You said your program crashes, did you run the program with your debugger? Your debugger should tell you exactly where it detects the problem and you should be able to view the variables at the time of the crash.

closed account (SEw0ko23)
I asked my lecturer whether it would be fine to use goto and return main(), and he told me it'll be fine for this project, however he did advise against it. Seeing as it is the 'easiest' to use, i decided to go for it.

I used the debugger and got the crash on the following line (line 481):
lvl1[x][y] = me;

This is in the void position function
I asked my lecturer whether it would be fine to use goto and return main(), and he told me it'll be fine for this project, however he did advise against it.

Well, IMO, your lecturer is wrong in regards to main(). The C++ standard specifically states that calling main() from any function, including main() is not allowed.

This is in the void position function
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
/*This finds the position of the player and where they are moving to*/
void position(int lvl) {
	int x, y;
	if (lvl == 1) {
		x = startingPointX(lvl, y);
		lvl1[x][y] = me;
	}
	if (lvl == 2) {
		x = startingPointX(lvl, y);
		lvl2[x][y] = me;
	}
	if (lvl == 3) {
		x = startingPointX(lvl, y);
		lvl3[x][y] = me;
	}
	if (lvl == 4) {
		x = startingPointX(lvl, y);
		lvl4[x][y] = me;
	}
	if (lvl == 5) {
		x = startingPointX(lvl, y);
		lvl5[x][y] = me;
	}
	if (lvl == 6) {
		x = startingPointX(lvl, y);
		lvl6[x][y] = me;
	}
}


So what is the value of x and y at the time of the crash?

As I noted above the program won't compile for me because of the calling of main() (along with several of the other Windowisms in your code) so you're going to need to help me help you with the debugging. My guess is that one or both of the values held in those variables is larger than the size of your arrays.


closed account (SEw0ko23)
Your guess is right, the values held in the variables are larger than the size of my arrays. I tried increasing the size of my arrays but still get the same crash.
I am not the best at debugging code, as i haven't done it much before, so sorry if my help to help you isn't the best.
the values held in the variables are larger than the size of my arrays. I tried increasing the size of my arrays but still get the same crash.

I suggest that you find out why the values are larger than your arrays and fix that problem.

What exactly are the values? Are both values (x and y) bad?

The next place to look is probably startingPointX()?

What happens if if (lvl1[i][j] == 'X') { never evaluates to true? You seem to return zero (at the end of the function) for x but what about y (it never gets modified).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*The starting point of 'X' is where the '@' is positoned when the level has loaded*/
int startingPointX(int lvl, int &y) {
	int xCoord;
	if (lvl == 1) {
		/*This is for level 1*/
		for (int i = 0; i != 20; ++i) {
			for (int j = 0; j != 20; ++j) {
				if (lvl1[i][j] == 'X') {
					xCoord = i;
					y = j;
					return xCoord;
				}
			}
		}
	}
...
	return 0;
}

Also you may want to see if it's possible to reduce the complexity of this function, there seems to be quite a bit of duplicate code.


By the way that function name is misleading since the function is modifying both x (through the return value) and y (through the reference parameter).



closed account (SEw0ko23)
I am unable to find the values of x and y during the crash. However, one thing I have tried is changing the numbers in positionFinder and startingPointX from 20 to 1000, so it looks like this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int startingPointX(int lvl, int &y) {
	int xCoord;
	if (lvl == 1) {
		/*This is for level 1*/
		for (int i = 0; i != 1000; ++i) {
			for (int j = 0; j != 1000; ++j) {
				if (lvl1[i][j] == 'X') {
					xCoord = i;
					y = j;
					return xCoord;
				}
			}
		}
	}


and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int positionFinder(int lvl, int &y) {
	int xCoord;
	if (lvl == 1) {
		/*This is for level 1*/
		for (int i = 0; i != 1000; ++i) {
			for (int j = 0; j != 1000; ++j) {
				if (lvl1[i][j] == '@') {
					xCoord = i;
					y = j;
					return xCoord;
				}
			}
		}
	}


This stops the game from crashing when restarting it, however the @ character has a starting position right next to the O, which is the exit, rather than its original palce
I am unable to find the values of x and y during the crash.

Why not? What compiler/IDE are you using?

Have you tried printing the values of x and y in the position() function, before the assignment?
1
2
3
4
5
6
7
void position(int lvl) {
	int x, y;
	if (lvl == 1) {
		x = startingPointX(lvl, y);
                //////////////////   Print the values of lvl, x and y here.
		lvl1[x][y] = me;
	}


Since you know where the problem occurs, set a breakpoint a few lines earlier and then single step through the code in question, be sure to step into any functions that are being called.

I have tried is changing the numbers in positionFinder and startingPointX from 20 to 1000, so it looks like this now:

That is surely wrong since your arrays are only size 20, so what you've accomplished is moving the out of bounds array access into these functions.



closed account (SEw0ko23)
the values of x and y outputted when running the level the first time is: X = 0 and Y = 20. When running the level the second time, it outputs X = 0 and Y = 660, but this is if i leave 1000 in the two functions, instead of 20.
If change it back to 20 in startingPointX and PositionFinder, and run the level for the first time, it outputs the value of X= 1 and Y= 0. When i run the same level for the second time, it outputs the value of X= 0 and Y= -858993460
the values of x and y outputted when running the level the first time is: X = 0 and Y = 20.

Well this is incorrect. y should be less than 20, remember arrays start at zero and stop at size - 1. So the maximum value either of these values should be 19.

When running the level the second time, it outputs X = 0 and Y = 660, but this is if i leave 1000 in the two functions, instead of 20.

This is even worse. When you throw that 1000 into the mix your overflowing your arrays inside that function so anything after that is garbage. You can't access 1000 elements if you only have 20 elements in your array!

If change it back to 20 in startingPointX and PositionFinder, and run the level for the first time, it outputs the value of X= 1 and Y= 0.

I don't know what you expect but these values are within the range of your arrays so the program shouldn't crash.
When i run the same level for the second time, it outputs the value of X= 0 and Y= -858993460

Okay, now this is a problem, array indexes can never be negative. By the way looking at the magnitude of the number this looks like an uninitialized variable. So did you single step through these loops with your debugger? It looks like y is never assigned a value, probably because lvl1[i][j] is never equal to '@'.
closed account (SEw0ko23)
I don't know what you expect but these values are within the range of your arrays so the program shouldn't crash.

The program doesn't crash here, it only crashes when it outputs the random value of y, which gets outputted on the second time i select to run the level.

By using the going single step through the debugger, I am unable to get anything, it just shows that the game crashes on line 48.

How would i assign a value to y and make the line lvl1[i][j] equal to '@'
By using the going single step through the debugger, I am unable to get anything, it just shows that the game crashes on line 48.

That doesn't sound like you're single stepping. How did you set the breakpoint in your code so that the debugger stops before it reaches the code in question? How are you telling your debugger to move to the next line?

You still haven't told me what compiler/IDE you're using so there isn't much else I can help you with.

How would i assign a value to y and make the line lvl1[i][j] equal to '@'

I have no idea. This is your code I would think you would know where '@' should be located in your array. But I can see that that character in not present in your initial array, so where are you adding that character into your array?

softwareguy wrote:
I asked my lecturer whether it would be fine to use goto and return main(), and he told me it'll be fine for this project, however he did advise against it.


IMO, that is BS: Any self respecting prof would strongly advise against doing those things.

This is looking a lot like a trolling topic, you aren't being helpful.

closed account (SEw0ko23)
i'm not trolling, I genuinely needed help with the game. The reason I was not being helpful is because i'm not the best at giving help with code, as I am very very new to C++. I have submitted my project now. I appreciate the help anyway, you have been very helpful.
Topic archived. No new replies allowed.