I'm learning C from a book and they have this sample code:

Code:
#include <stdio.h>

#define DIM 3
#define SPACE ' '

void inic(char s[ ][DIM])
{
   int i,j;
   for(i=0; i<DIM; i++)
   for (j=0; j<DIM; j++)
   s[i][j] = SPACE;
}

void show (char s[DIM][DIM])
{
   int i, j;
   for (i=0; i<DIM; i++)
   {
      for ( j=0; i<DIM; j++)
      printf("%c %c", s[i][j], j==DIM-1?' ':'|');
      if (i!=DIM-1)
      printf ("\n--------");
      putchar('\n');
   }
}
main()
{
   char TicTacToe[DIM][DIM];
   int posx, posy;
   char ch = '0';
   int n_plays = 0;

inic(TicTacToe);
while (1)
{
   show(TicTacToe);
   printf("\nInput the coordinates of your move row column: ");
   scanf("%d %d", &posx, &posy);
   posx--; posy--;
   if (TicTacToe[posx][posy] == SPACE)
   { TicTacToe[posx][posy] = ch = (ch== '0') ? 'X' : '0';
   n_plays++;
   }
   else
   printf("Already filled position\n Try again!!\n");
   if (n_plays==DIM*DIM)
   break;
}
show(TicTacToe);
}



I copied it exactly the way it was in the book, I double checked it, I saved it with the extension .c and the compiler doesn't give me any error, yet when I run it the console displays a bunch of gibberish and another window pops up saying that the cmd crashed. What could be causing this?
Every program in the book so far worked just fine, maybe it's my computer?
Be very careful when nesting for loops, it is very easy to accidentally use the wrong variables.


Code:
   for (i=0; i<DIM; i++)
   {
      for ( j=0; **i**<DIM; j++)
      printf("%c %c", s[i][j], j==DIM-1?' ':'|');

The i in the inner for loop should be a j. That then compiles and runs as expected.
Thank you, it works now!

Actualy, the book is correct, it was me who swapped the j for an i, oops
yea thats why when i use variables i always make them uppercase. it makes them a lot harder to confuse like that.
The convention in C is to have variables that at least start with a lowercase letter, like i or j or some var or some_var. Also, AliceIsDead, considering tabbing in your code when you make loops to make it clearer for yourself to read what's going on. Neatly-styles code can be a great early way to catch bugs.
Ah. makes sense. i know C++ you could use capitals.
For C and C++: Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit.

GCC has an extension for C naming rules such that $ is a valid character for identifier names.

GCC also has experimental support for unicode characters in identifiers.

Certain identifier naming conventions are reserved either by the C/C++ standards or the compiler. SO answer for reserved names
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement