Variable value NOT returned from function

I wrote a Nonogram game, also known as Picross or Griddlers, that are logic puzzles.. Anyway, the main function is this..
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

int main()
{
	SetConsoleTitle(TEXT("\"Nonogram\""));
	string line(58,'\xC4');
	int nonogame[21][21] = {{0},{0}}; // 0 to 19 for puzzle, #20 for amount of filled grid cells in row or column
	int rows, columns;
	int over, down;
	char again = 'N';
	Console::SetWindowSize(width+1, height+1);
	Grid(1,0,0,width-1,height-1,1,1,black, light_gray,0,0,0);
	gotoXY(34,0,"\xB6 Nonogram \xC7");
	do
	{
		rows = 3, columns = 3;
		Set_Grid_Size(rows, columns); 
		over = (width-(columns*2))/2;
		down = (height-(rows*2))/2;
		Grid(0,1,1,width-3,height-3,1,1,black, light_gray,0,0,0);
		Grid(3,over,down,1,1,columns,rows,black, light_cyan,1,dark_gray,dark_gray);
		Grid(3,12,47,56,1,1,1,black, light_cyan,1,dark_gray,dark_gray);
		text(black on dark_gray);
		gotoXY(13,50, line);
		Set_Grid_Fill(over, down, rows, columns, nonogame);
		Fill_Grid(over, down, rows, columns, nonogame);
		again = Play_Again();
		gotoXY(2, 6, "?? Again = "); // Never prints
		cout << again; // This is to check what is returned
	}while(again == 'Y');
	setcursor(1, 10);
	text(black on light_gray);
	WaitKey();
}


Play_Again function is..

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
char Play_Again()
{
	text(black on light_cyan);
	string You_Are_A_Winner = "C O N G R A T U L A T I O N S !!! You solved the Nonogram. - Press 'Y' to play again or 'N' to Quit - ";
	char Y_N, holder;
	int len = You_Are_A_Winner.length();
	
	do
	{
		for(int x = 0;x < 56; x++)
		{
			gotoXY(13+x, 48);
			cout << You_Are_A_Winner[x];  // scrolling the string called 'You_Are_A_Winner'
		}

		holder = You_Are_A_Winner[0];
		for (int x = 0; x < len; x++)
			You_Are_A_Winner[x] = You_Are_A_Winner[x + 1];
		You_Are_A_Winner[len] = holder;
		Sleep(100);
		if (_kbhit()) // A Key is pressed
		   Y_N = toupper(_getch());
	}while (Y_N != 'N' && Y_N != 'Y');// Checks if key is 'Y' or 'N'
	
	gotoXY(3,2, "Y_N = "); // Prints on screen. Checking what Y_N contains
	cout << Y_N; // Does print a 'Y' or 'N', depending 
	cout << endl << "Returning 'Y_N'"; // Also prints on screen
	return Y_N; 
}


When game is filled in correctly, the text 'C O N G R A T U L A T I O N S !!! You solved the Nonogram. - Press 'Y' to play again or 'N' to Quit - ', does scroll. Pressing the 'Y' or 'N' causes the text to print at the end of the function, but Main is not jumped to, so the user cannot play another game, by choosing a new board size, etc. I'm stumped on WHY it's not working. If you need the whole code, I could put it in my dropbox with a D/L link to the source.

And before anyone asks, yes, I tried removing the scrolling, and use JUST a cin command for the Y_N variable. I had the same results as with the scrolling being used.

Thanks for any hint or answer. Not homework. Just a 68 year old that likes to program.
Since I can't run this myself, I must ask what debugging tools you have available.

I sense that there could be something happening in the manner of a buffer overrun, or something like that, which is actually changing the value of Y_N.

It may help a little to know the binary value of 'again' in main when it is not printing the Y or N.

An example of suspicious code is this:

1
2
3
4
5
6
7
8
9
int len = You_Are_A_Winner.length();
	
	do
	{
		for(int x = 0;x < 56; x++)
		{
			gotoXY(13+x, 48);
			cout << You_Are_A_Winner[x];  // scrolling the string called 'You_Are_A_Winner'
		


While the length of "You_Are_A_Winner" is taken, it isn't used. The loop for x moves through 55, but I have no way of know if there are in fact 55 entries in the array. If there isn't, that is the kind of thing I'm looking for here.
Theres this thing in a debugger where you can put a break point and when you run the code in the debugger the code will pause where that breakpoint is.

In your IDE while paused in a breakpoint there should be a button for moving to the next breakpoint, moving to the next instruction, and moving into a function on the current instruction (there may be more or less features depending on your IDE or debugger but all in all that is the gist of it).

That should tell you exactly what is going on.
@Niccolo

Actually, len IS used, in line 17 of the Play_Again function. It's used to take the first letter of the string and place it at the end of the 'You_Are_A_Winner' string, after shifting all the letters to the left. Then I print the first 56 letters onto the screen, since the 'You_Are_A_Winner' string is a mite longer..

I use Microsoft Express 2012. To debug, I print the variables after giving them values, as shown in lines 25 thru 27. I then try printing what value was passed, as shown in Main, lines 27 and 28. They never print though, so it seems, nothing is being returned. Or the return never happens. Strange!!
If that's Microsoft Visual Studio Express 2012, I think it has the feature to break on data change.

You can set a breakpoint to fire when Y_N is changed (it's a breakpoint that doesn't even actually have a line of code).

See if that tells you when it happens.
@Niccolo & @poteto

The problem isn't that the variable is being changed, it's that the return from the Play_Again function, isn't happening. I can print the Y_N char variable, which happens on lines 25 and 26, then I print the line "Returning 'Y_N'" on line 27. Line 28 is the return statement, which should send me to main, line 27, where it should print out the variable, again, on line 28. Those two lines never print, as I seem to be staying in the Play_Again function.

Are you confusing "doesn't print" with "doesn't stay on screen long enough for you to see it"?

1
2
3
4
5
		Fill_Grid(over, down, rows, columns, nonogame);
		again = Play_Again();
		gotoXY(2, 6, "?? Again = "); // Never prints
		cout << again; // This is to check what is returned
	}while(again == 'Y');


I don't know what the rest of your do-while loop does, but if runs quickly (has no UI interaction) and also manages to erase your "?? Again" message, then for sure it will seem like you're stuck in Play_Again().

Put a Sleep(5); in after your final cout, before the loop runs again.
@salem c

No, I'm not confusing the two. I don't clear the screen at any time. I have the variable printed to screen when I enter the value. It shows up.

Right now, it's 1 am where I live, so I'm headed to bed. Here's the source code for the game. Try it out, see if it works for you, ( or @Niccolo or @poteto, or anyone else)

In dropbox. D'L it.

https://www.dropbox.com/s/gtvjml8mqoji72u/Nonogram.cpp?dl=0
I ran the code in the debugger in VS2010 (version really doesn't matter here, I did remove namespace System though).

I got debug asserts on uninitialized values and going out of range x2.

After fixing those problems, it turns out you are looking at the incorrect location of where you are calling Play_Again() which could've been shown using breakpoints.

1
2
3
4
        if( correct_grid == rows + columns )
        {
            Play_Again();
        }


Breakpoints are very important for when you don't even know where your code is or how it is flowing. It also shows the variables inside of the scope of the function, and shows the full function stack This is also the same result you get from pausing the debug session, or catching a signal exception like segfault.
@poteto

I hadn't noticed I put that piece of code into the Fill_Grid() function. After changing it to
1
2
3
4
if( correct_grid == rows + columns )
 {
      win = true;
  }

the program starting working correctly.

Thanks to all for helping me out, and especially to you, @poteto
Topic archived. No new replies allowed.