Case of the Phantom Function: SOLVED!
What a difference a good night’s sleep can make to a problem - in coding as in so much else.
Coming back to my troublesome ‘zoomstring’ function with a fresh set of eyes, I decided that, rather than go through all my code line by line looking for the problem, I ought to take a step back and think about it logically.
The function is definitely getting called - I know this because of the debugging line I added at the start using cout to leave evidence of this in the command line console.
There’s nothing actually “wrong” with the code - it compiles without giving an error, and it doesn’t crash or break the game when called during runtime. It’s just as if nothing is happening.
So, logically, that leaves a problem with the function’s main loop.
Everything takes place within a big nested “for” loop. I stare for minutes on end at the line that starts the loop off.
for (int n=1; n==9; n++)
Looks fine to me. But maybe I’d better just check - ACK!
Schoolboy error. I’d thought that for loops checked for their condition and continued until the condition was true. But a quick check of one of my earliest tutorial lessons tells me that in fact the for loop only continues while the condition is true. So, since n isn’t equal to 8 when the loop starts, everything else in the function gets completely ignored and the program goes back to where it was before!
A quick rewrite followed:
for (int n=1; n<=8; n++)
That’s more like it. As soon as I ran the game again, text zoomed onto the screen every time it was supposed to. Turns out everything else in the function worked perfectly. It’s just that I’d misunderstood one of the fundamentals of C++. No biggie.
Piece of cake, this debugging lark.