Variable equals one but outputs 10 or greater

I am working on making minesweeper for the fun of it, but I'm having some serious issues. I have this code on another thread, but I don't even know if it was published because it was too long initially. I'm initializing a variable at 1, but for some reason, the executed code never uses anything less than ten for when I call the variable. There are some other big problems, but I think that this is mostly a compiler issue. I think this because my compiler has been doing some weird things lately. The first thing that I really need is someone to compile and run my code, then post output to my other thread. Then, if the compiler wasn't the problem, I need someone to take a second look at my code and help me find what's wrong with the code. I wouldn't have made a second thread, but it's been two days and I can't continue any work on this without a solution. I've tried a lot of things to fix some problems but haven't gotten anywhere.

Here's the address I have for the other thread.
http://www.cplusplus.com/forum/beginner/72410/
I'm initializing a variable at 1, but for some reason, the executed code never uses anything less than ten for when I call the variable.


It might be useful if you said which variable and perhaps indicated the line in code where it may be found.

You aren't making it easy to help you by being stingy with information.
Last edited on
Right now my main task is to make sure my compiler is running correctly. It has been doing weird things lately like trying to process numbers that my if statement should've made invalid input.

The variable initialized at one is zone within the zone method, but I don't even do anything with it until I hit certain circumstances and the value has already changed most times.
It's not impossible to stumble upon a compiler bug, but it's really, really unlikely. If you have serious doubts about the correctness of the compiler output, try using a different compiler and see if the program behaves properly then.

I haven't looked too closely at your code, but if I had to venture a guess, I'd say you screwed up in one of the dozens of places where you're doing something of the form
array[another_array[a]][yet_another_array[b]]
This style of indirection is a double edged sword: it can be used to make a very elegant solution to a complex problem, but it's also very easy to screw up and damn near impossible to understand and debug. It's the array equivalent of spaghetti code.
Personally, I wouldn't even bother debugging. It's only a couple hundred lines, so you should be able to rewrite it in a few hours.
I don't have a problem with that part because I've already proven that that part works. The other array (there are only two) holds the coordinates entered by the user that allows the program to access the data in the first array at those coordinates.

The real problem occurs in my count and zone methods. I can't tell what's wrong with them at all. I have worked with the code I used for those methods before without issues, but for some reason something went wrong this time. I've had problems with my compiler before, so I don't have too much faith in it when this happens, and my other compiler can't use transform().
I've already proven that that part works
Do you mean you made a formal proof of the correctness of that part of the program, or that you've previously tested it and found that it didn't make the program error out? If the latter, it doesn't prove anything. Just because the program didn't crash doesn't mean the code is correct.
Either way, you could still be making a mistake. As Knuth said,
Beware of bugs in the above code; I have only proved it correct, not tried it.


If you're experiencing seemingly random crashes or incorrect results, it could suggest memory corruption. Writing outside the boundaries of an array doesn't always make the program crash immediately. The errors can linger in memory and the program can crash later on, often in a part of code that's totally unrelated to the one that actually contains the bug.

What compilers are you using?
When memory issues are involved, no "parts" of the program exist anymore. Writing beyond array bounds in one part of the program can mess up entirely unrelated parts of the program (like magically changing values of variables).
This is one of the reasons you should never use raw arrays in your program. When you use STL containers, you can easily enable debug iterators for a moment, which allows you to see easily whether you have any out-of-bounds accesses. I'd start with replacing all C arrays with std::array or std::vector.
By proven, I mean that I have run this program and the part of the code with the two arrays works exactly as I want it to. I don't think I have any part of the code where I haven't done everything I know to stop the code from even looking past the end of the array. My only problem does not have anything to do with exceeding bounds of the array because the variable I initialize is initialized after I have done finished my first set of processing the array, then it will send numbers greater than ten before I have had a chance to even exceed the bounds of the array.


| 10 | 3 | 11 | 12 | 13 | 14 | 15 | 16 | 0 |
| 17 | 0 | 18 | 18 | 13 | 19 | 20 | 21 | 0 |
| 17 | 22 | 23 | 23 | 0 | 19 | 24 | 25 | 0 |
| 0 | 22 | 26 | 26 | 0 | 0 | 27 | 28 | 0 |
| 0 | 0 | 26 | 26 | 0 | 29 | 30 | 31 | 0 |
| 0 | 32 | 32 | 0 | 33 | 34 | 35 | 36 | 0 |
| 38 | 39 | 39 | 0 | 40 | 41 | 42 | 43 | 0 |
| 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
a  | # | # | # | # | # | # | # | # | # |
b  | # | # | # | # | # | # | # | # | # |
c  | # | # | # | # | # | # | # | # | # |
d  | # | # | # | # | # | # | # | # | # |
e  | # | # | # | # | # | # | # | # | # |
f  | # | # | # | # | # | # | # | # | # |
g  | # | # | # | # | # | # | # | # | # |
h  | # | # | # | # | # | # | # | # | # |
i  | # | # | # | # | # | # | # | # | # |

   | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Enter coordinates (exit to end):


This is what my first output is before I have done anything. That ten in the top left corner of the board that is meant for displaying the zones of each element in the array should not be more than one because it is the first element that was ever checked. What's more worrying is that the next number goes down to 3 when there is no part of my code that subtracts from three. Then the numbers seem to follow my code except that it isn't performing some parts of the two for loops in the zone method that are vital in establishing these zones.

I use Visual Studio 2008 and Dev C++, but I don't like Dev anymore.
I'd take a very, very hard look at Minesweeper::position(). The logic looks very murky there, specially around the 'copy' array.
Also note that there's a possible case of infinite recursion.

There's a bug in Minesweeper::zone(). You're initializing your min-max variables assuming that the fors will execute the max case (i.e. that the relational is <=), but you're using < in the loops. So, if min=0 and max=1, the loop will not run only check array[x], but not array[x+1].
Last edited on
I noticed that. It's fixed now though and everything works exactly as it should. Thanks for your help.
Not so perfect actually. I'm having new problems that are more confusing. I completely revamped the zone method and I am having new problems that happen randomly. Again I've initialized zone at one and it will skip numbers randomly as well as other problems.
Topic archived. No new replies allowed.