string subscript out of range

I'm writing a program where I can input a board from http://www.vivalagames.com/play/reactioneffect/fullscreen.php and it'll run through all moves and give me the best one to play. However, I'm getting an error that says "Debug Assertion Failed! string subscript out of range". Google says this means that I'm trying to access part of the array that isn't there. However, looking through my code I don't see where. By commenting out the code that displays the board the program runs fine. Specifically, this snippet, which is in for loops to get a specific area of the board to input the type of curve.

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
for( int dispX = 0; dispX < BOARD_X; dispX++ )
{	
	for( int dispY = 0; dispY < BOARD_Y; dispY++ )
	{
		message = "";
		switch( board[dispX][dispY] )
		{
		case 0:
			message += mess0;
			break;
						
		case 1:
			message += mess1;
			break;
						
		case 2:
			message += mess2;
			break;
						
		case 3:
			message += mess3;
			break;

		default:
			break;
		}
					
		if(( dispX == curX ) && ( dispY == curY ))
		{
			Font.show_text( curX * Font.get_cellW() , (curY * Font.get_cellH()) + 100, message, screen, YELLOW, RED );
		}
		else
			Font.show_text( dispX * Font.get_cellW(), (dispY * Font.get_cellH()) + 100, message, screen, WHITE, BLACK );
			 
	}
}


Font is a modification of Lazy Foo's bitmap font tutorial (http://lazyfoo.net/SDL_tutorials/lesson30/index.php) that I added foreground and background color to, it works fine in my other programs. mess0-3 and message are declared earlier in the code:
1
2
3
4
5
6
7
//for display
std::string message;
	
unsigned char mess0 = 0xC0;
unsigned char mess1 = 0xDA;
unsigned char mess2 = 0xBF;
unsigned char mess3 = 0xD9;


What am I missing?
The problem is in the show_text function.
for( int show = 0; text[ show ] != '\0'; show++ )

In debug mode VC++ will complain that you try to access text[text.size()]. This is explicitly allowed in the latest C++ standard so I would consider this a bug in VC++.

As a work around you can change the loop condition to check show against text.size() instead.
for(std::size_t show = 0; show < text.size(); show++)
(std::size_t is used to avoid warnings about signed/unsigned comparison)
Last edited on
That's got it, thanks for the help.
Topic archived. No new replies allowed.