• 0

Weird Bug (Console)


Question

Im working on a simple Console based Tic Tac Toe game, just for learning experience, and after i compiled and ran it it did something weird. (View screenshot)

The smiley looking characters at the end of each dashed line, is not located in my program, and I cannot see why it would appear.

#include <iostream>
using namespace std;

int main() {

	char thecmd[50];
	int set[4][4], x, y;
	int turn = 1;
	char bigline[50];
	strcpy(bigline, "------------------------------------------------------------");

	for ( x=1; x<=3; x++ ) {
  for ( y=1; y<=3; y++ ) {
 	 set[x][y] = 0;
  }
	}

	while ( strcmp(thecmd,"exit") ) {

  if ( turn==1 ) { turn = 2; }
  else { turn = 1; }

  cout << "Command: ";
  cin.getline(thecmd,50);
  cout << "\n";

  if ( !strcmp(thecmd,"go 1") ) { set[1][1] = turn; }
  if ( !strcmp(thecmd,"go 2") ) { set[1][2] = turn; }
  if ( !strcmp(thecmd,"go 3") ) { set[1][3] = turn; }
  if ( !strcmp(thecmd,"go 4") ) { set[2][1] = turn; }
  if ( !strcmp(thecmd,"go 5") ) { set[2][2] = turn; }
  if ( !strcmp(thecmd,"go 6") ) { set[2][3] = turn; }
  if ( !strcmp(thecmd,"go 7") ) { set[3][1] = turn; }
  if ( !strcmp(thecmd,"go 8") ) { set[3][2] = turn; }
  if ( !strcmp(thecmd,"go 9") ) { set[3][3] = turn; }


  for ( x=1; x<=3; x++ ) {
 	 for ( y=1; y<=3; y++ ) {
    cout << set[x][y];
 	 }
 	 cout << "\n";
  }

  cout << "\n" << bigline << endl;

	}

	return 0;

}

I am using Windows XP professional. I used Visual C++ v6.0 to compile it, and the source above, after recompiling, kept this weird error.

Does anyone know why this happens, or could they compile it if they use Visual c++, and tell if the same or similar error happens?

post-47-1086853925.jpg

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

hmm, u are placing a 60 character string in a 50 character array

strcpy(bigline, "------------------------------------------------------------");

not sure if its that the problem, but its not good to overflow an array.

Link to comment
Share on other sites

  • 0

Ohhh woops, I just realised after u posted that it was probably that i hadnt allowed enough room in the bigline[] array, still kinda weird that it showed a smiley though, anyone got a reason why it was a smiley, and not a mooshed up piece of junk?

Link to comment
Share on other sites

  • 0

i suppost you where trying to access a piece of memory which you are not allowed to.

if you placed the bigline[] at the top, you would have seen the contents of the other before the other declarations.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.