Archived

This topic is now archived and is closed to further replies.

TyrantKing

C++ ascii display nightmares

Recommended Posts

I am trying to display a game board using ascii characters. My initial attempts were successful, but something strange has happened, and I don''t know what to do. I am storing information in a two-dimensional array of a user-defined object type. Within this object class is a character code that contains the ascii number value corresponding to the symbol I want to be displayed. I have overloaded the << operator to display the symbol simply by entering cout << objectVariable; I would run some nested for loops to set the appropriate values into the appropriate objects in the array, and display the array using nested for loops with the overloaded output operator. This seemed to work how I planned, however, when I tried to implement the next part of the program everything fell apart. I needed different data to be stored in different objects, so I used public inheritence to create additional types of objects, and converted the array of objects into an array of pointers to objects. I adjusted the initialization and display loops to handle the pointers, but something has gone wrong with the display. I have not put any of the new objects into the array ( I am still using the original object types ), but three-quarters of the way through the display the ascii characters begin to break down. I have tried setting them up as unsigned chars (as most of the codes I need are around the 200 mark), and have tried using signed characters converting the high codes into their negative, signed equivalent. I have tried static casts and tried running the codes as integers and then casting them as characters during output, but to no avail. I am at a complete loss, as it works most of the way through the display. The array is 27 x 43, and it works all the way up until [22][36]. At array index [22][37] in goes beserk, and randomly changes the ascii values. Any feedback on what could be causing this would be greatly appreciated!

Share this post


Link to post
Share on other sites
You said you had now changed your program to use pointers. The error you describe may be the result of some pointer pointing into your array data and corrupting it by changing it.

Just an idea. Without seeing the code it is hard to tell what you did wrong.

Jacob Marner, M.Sc.
Console Programmer, Deadline Games

Share this post


Link to post
Share on other sites
I''m not sure that a rogue pointer could sneak into the middle of the data, it''s possible I suppose, but I''m not sure how it would get in. As I said, it works fine up until one particular spot, but I can''t find anything about that spot that makes it any different than the rest of the code.

I''m setting up the initial pointers something like this:

Object_T emptyArray[ ROW_SIZE ][ COL_SIZE ];

for( int row( 0 ); row < ROW_SIZE; row++ )
{
for( int col( 0 ); col < COL_SIZE; col++ )
{
Object_T * ptrObj;

try
{
ptrObj = new( Object_T );
}
catch(bad_alloc)
{
cout << "\nunable to get enough memory\n";
exit(1);
}
ptrObj = &emptyArray[ row ][ col ];
objArray[ row ][ col ] = ptrObj;
}
}

The values are getting set as follows(just a snippet):

/////////////middle//////////////////////
for( int middle( 3 ); middle < ROW_SIZE - 1; middle++ )
{
for( int midCol( 3 ); midCol < COL_SIZE - 1; midCol++ )
{
if( ( (middle + 4) % 3 == 0 ) && ( (midCol + 3) % 5 == 0 ) )
{
objArray[ middle ][ midCol ]->displayCode = -59;//197;
objArray[ middle ][ midCol ]->type = BOARD;
}
else if( (middle + 4) % 3 == 0 )
{
objArray[ middle ][ midCol ]->displayCode = -60;//196;
objArray[ middle ][ midCol ]->type = BOARD;
}
else if( (midCol + 3) % 5 == 0 )
{
objArray[ middle ][ midCol ]->displayCode = -77;//179;
objArray[ middle ][ midCol ]->type = BOARD;
}
else
{
objArray[ middle ][ midCol ]->displayCode = '' '';
objArray[ middle ][ midCol ]->type = EMPTY;
objArray[ middle ][ midCol ]->playerOwnership = NONE;
}
}
}

(please pardon the improper format, I''m trying to fit it into the lines a tad better)

The display is done so:

for( int i( 0 ); i < ROW_SIZE; i++ )
{
StepOver();

for( int j( 0 ); j < COL_SIZE; j++ )
{
SetColour( *(objArray[ i ][ j ]) );
cout << *(objArray[ i ][ j ]);
}
cout << endl;
}

StepOver is a function that simply adds a \t before the display begins, to center the game board on the console window.

As stated before, << has been overloaded to output the displayCode of an object to the screen.

Hope this sheds some light on my befuddled brainwaves, and thanks for the suggestion.

Share this post


Link to post
Share on other sites
If you want to post code use the [.source.] Code goes here...[./source.] tags.. just remove the periods. Or look in the forum faq. Just press the little question mark with faq by it.

Edit:

Heh, I posted forum fag the first time

[edited by - Zefrieg on August 14, 2002 4:10:43 AM]

Share this post


Link to post
Share on other sites
Thanks, I will use that will including code samples.

Could it be possible that the output stream buffer is getting full and causing the problems? Could that be why it works most of the way through and then just chokes towards the end? If so, what would be the best way to flush the buffer?

Share this post


Link to post
Share on other sites
haven't read the whole thing through but i'm curious as to what you think you are doing at the begining


    
Object_T emptyArray[ ROW_SIZE ][ COL_SIZE ];

for( int row( 0 ); row < ROW_SIZE; row++ )
{
for( int col( 0 ); col < COL_SIZE; col++ )
{
Object_T * ptrObj;

try
{
ptrObj = new( Object_T );
}
catch(bad_alloc)
{
cout << "\nunable to get enough memory\n";
exit(1);
}
ptrObj = &emptyArray[ row ][ col ];
objArray[ row ][ col ] = ptrObj;
}
}


the next to last line
ptrObj = &emptyArray[ row ][ col ];
is dubious. you have already assigned a new object to this pointer and are losing track of it by including this line.

either just miss the line out or miss the line with 'new (Object_T)'.

you don't need both and the normal way in my book is to use 'new'

[edited by - petewood on August 14, 2002 3:15:37 PM]

Share this post


Link to post
Share on other sites
I think I have isolated the problem. Something is happening in the display board function. The function looks like this(with much commented out to help me track down where it was breaking down exactly):

    
void Game_T::DrawObjBoard()
{
cout <<"\n\nat start of draw board, it's "
<< objArray[ 22 ][ 38 ]->displayCode <<endl;

for( int i( 0 ); i < ROW_SIZE; i++ )
{
// StepOver();


for( int j( 0 ); j < COL_SIZE; j++ )
{
// SetColour( *(objArray[ i ][ j ]) );

// cout << objArray[ i ][ j ]->displayCode;

}
// cout <<endl;


cout <<"\n\nin the loop, for row number: " <<i <<" it's "
<< objArray[ 22 ][ 38 ]->displayCode <<endl;
}
}

I have commented out the overloaded output operator and am now just displaying the symbols directly (again, to isolate the problem). The output at the start of the function, the "at start of draw board" line has the proper display. However, once it enters the loop it changes. Even though the loop doesn't do anything anymore(it's all commented out), as soon as it hits the "in loop" output line the display has changed. It then retains this new display all the way through the rest of the loop.

I would have thought that if something in the loop changed the display(although I don't know what could), it would keep changing it as it went through. However, that is not the case. It changes it at entry and keeps the new value all the way through.

Any ideas?

[edited by - TyrantKing on August 14, 2002 3:28:56 PM]

Share this post


Link to post
Share on other sites
Wow, petewood found the problem.

I set that line up to give me a physical location to access the information while I was hunting down another bug, which I found, but I never thought to take it back out.

Thank-you very much, that is a great help.

If someone has some spare time, I wouldn''t mind having an explanation as to why that was causing the bug in that specific location, just to help increase my personal understanding of the problem. I have only been coding for 4 months, so some of the pointer and memory management stuff is still a bit fuzzy.

Again, thanks to all who took the time to reply.

Share this post


Link to post
Share on other sites