Thread: character array issue

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    character array issue

    Trying to make a simple tic tac toe game and I'm stumped, something is not working right.
    The grid is drawn easily, but I can't alter the grid for some reason or printf the proper character after getting input. It's probably something very simple and stupid, but its been a long time since doing any programming.

    Code:
    #include <stdio.h>
    
    	/* draw the grid */
    void draw_grid(char *grid) {
    	printf("\n");
    	printf("%c -- %c -- %c\n",grid[0],grid[1],grid[2]);
    	printf("%c -- %c -- %c\n",grid[3],grid[4],grid[5]);
    	printf("%c -- %c -- %c\n",grid[6],grid[7],grid[8]);
    	printf("\n");
    }
    
    int get_move(void) {		/* Bugs out if more than 1 character is entered, but have to do this to learn */
    	int x = '\0';			/*	ansi-C and not mess with platform specific solutions. */
    	int new_line = '\0';
    	x = getchar();
    	new_line = getchar();
    	return x;
    }
    
    int main(void) {
    	int x = 0;
    	char grid[9];
    	
    		/* initialize the grid */
    	for(x = 0; x < 9; x++) {						/*this part works due to the initial grid being printed properly */
    		grid[x] = '.';
    	}
    
    	while(1) {
    		draw_grid(grid);
    		x = get_move();
    		printf("Value of X: %c\n",x);
    		printf("Value of grid[x]: %c",grid[x]);		/* random garbage being printed here instead of '-' character. */
    		if(grid[x] != 'X') {						/* this doesn't appear to work at all */
    			grid[x] = 'X';
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    printf("Value of grid[x]: %d",grid[x]); /* random garbage being printed here instead of '-' character. */
    if(grid[x] != 'X') { /* this doesn't appear to work at all */
    The value of x is THE ASCII value of the character, so for the numbers 0-9 you get actually 48-57, so after you enter 2, for example, you try to access grid[50], which is well beyond the bound of the array.

    so you have to convert x by atoi(&x); or do it manually by defining index = x - 48. 48 is the value of the 0 character and so on.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int get_move(void) {		
      x = getchar() -'0';
       getchar();
       return x';
    }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    15
    Heh, that was incredibly simple. /facepalm.

    Code:
    if(grid[x-'0'] != 'X' || grid[x-'0'] != 'O') {
    			grid[x-'0'] = 'X';
    		}
    Did the trick. Thanks for the help.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by deepcode View Post
    Heh, that was incredibly simple. /facepalm.

    Code:
    if(grid[x-'0'] != 'X' || grid[x-'0'] != 'O') {
    			grid[x-'0'] = 'X';
    		}
    Did the trick. Thanks for the help.
    if you use...
    Code:
    x = getchar() - '0';
    and initialize your array properly
    Code:
    char grid[9] = {' '};  // initialize to spaces
    your mess abvove becomes...
    Code:
    if (grid[x] == ' ')       // space == unoccupied
      grid[x] = 'X';
    One step taken at the beginning eliminates multiple steps at the end.
    Last edited by CommonTater; 01-27-2011 at 08:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PIC16F684 Interfacing with Hitachi 44780
    By JTerry in forum C Programming
    Replies: 36
    Last Post: 12-13-2010, 12:13 PM
  2. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  3. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  4. watching character array in visual studio
    By neandrake in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2006, 11:12 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM