Thread: Problem with output

  1. #1
    Unregistered
    Guest

    Problem with output

    I recently wrote this program that displays (command-line program) a tic-tac-toe format to be played. I had started the program and wrote the function to print out the board with the specified occupied spots (an array). I tested it out, and I got a weird printout with numbers instead of the ' ' char or the 'O' char. Can you please take a look and tell me what's wrong?:
    Code:
    /****************************************************
    	TicTacToe -- a program to simulate the game *
    ****************************************************/
    
    #include <stdio.h>
    
    /***Function Prototypes***/
    void printbrd(int pieces[3][3]);
    
    int main()
    {
    	int pieces[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
    
    	/**print out the board for first time viewing**/
    	printbrd(pieces);
    
    	return 0;
    }
    
    void printbrd(int pieces[3][3])
    {
    	char lines[3][20] = {"( ) ( ) ( )", "( ) ( ) ( )", "( ) ( ) ( )"};
    	char space[3][3];
    	int i, j;
    
    	for (i = 0; i < 3; i++) {
    		for (j = 0; j < 3; j++) {
    			if (pieces[i][j] == 1)
    				space[i][j] = 'O';
    			else if (pieces[i][j] == 0)
    				space[i][j] = ' ';
    		}
    		j = -3;
    		sprintf(lines[i], "(%c) (%c) (%c)", &space[i][j], &space[i][j+1], &space[j+2]);
    	}
    
    	/**print out the lines that are pre-configured to the right**/
    	printf("%s\n", lines[0]);
    	printf("%s\n", lines[1]);
    	printf("%s\n", lines[2]);
    }
    Thanks!

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    try this:

    sprintf(lines[i], "(%c) (%c) (%c)", space[i][j], space[i][j+1], space[i][j+2]);

    alex

  3. #3
    Unregistered
    Guest
    Nope. Still doesn't work. Anything else that is wrong? Thanks for the reply!

  4. #4
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    sprintf(lines[i], "(%c) (%c) (%c)", space[i][0], space[i][1], space[i][2]);

    this should work...
    alex

  5. #5
    Unregistered
    Guest
    Nope, still doesn't work. The same distored output. But, thanks for the reply. I can't figure it out. It is probably something with my multidimensional array's definitons/implementations. Thanks, though!

  6. #6
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    I was quite convinced that that was the problem...
    This code works flawlessly here:
    Code:
    #include <stdio.h>
    
    /***Function Prototypes***/
    void printbrd(int pieces[3][3]);
    
    int main()
    {
       int pieces[3][3] = {{1,0,0}, {0,0,1}, {0,1,0}};
    
       /**print out the board for first timeviewing**/
       printbrd(pieces);
    
       return 0;
    }
    
    void printbrd(int pieces[3][3])
    {
       char lines[3][20] = {"( ) ( ) ( )", "( ) ( ) ( )", "( ) ( ) ( )"};
       char space[3][3];
       int i, j;
    
       for (i = 0; i < 3; i++) {
          for (j = 0; j < 3; j++) {
             if (pieces[i][j] == 1)
                   space[i][j] = 'O';
             else if (pieces[i][j] == 0)
                   space[i][j] = ' ';
          }
          sprintf(lines[i], "(%c) (%c) (%c)", space[i][0],
                space[i][1], space[i][2]);
       }
    
       /**print out the lines that are pre-configured to the right**/
       printf("%s\n", lines[0]);
       printf("%s\n", lines[1]);
       printf("%s\n", lines[2]);
    }
    alex

  7. #7
    Unregistered
    Guest
    Thanks for the code!!! Hmmm...I can't see it. What did you change from my code? I should know that for future reference so I don't make that mistake again. Thanks, again, for the code! I really appreciate it, alex.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my output
    By Dogmasur in forum C Programming
    Replies: 17
    Last Post: 08-07-2008, 08:07 PM
  2. Hash Table Problem.
    By penance in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 01:03 PM
  3. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  4. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM