variables with brackets??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

variables with brackets??

 
0
  #1
Jan 10th, 2009
Hi everyone. I'm currently working on a Tic Tac Toe game, and I am having a little trouble with, well, I'm not even sure what to call them. They are a variable with brackets like so below. Anyway, I have this variable:

  1. char Line_One[3] = { ' ', ' ', ' ' };

What is the significance of the 3 in the brackets?
Also, how would I change one of the characters in the curly brackets?
for ex.:
  1. char Line_One[3] = { ' ', ' ', ' ' };
turns into
  1. char Line_One[3] = { 'X', 'X', 'O' };

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: variables with brackets??

 
0
  #2
Jan 10th, 2009
Originally Posted by besktrap View Post
Hi everyone. I'm currently working on a Tic Tac Toe game, and I am having a little trouble with, well, I'm not even sure what to call them. They are a variable with brackets like so below. Anyway, I have this variable:

  1. char Line_One[3] = { ' ', ' ', ' ' };

What is the significance of the 3 in the brackets?
Also, how would I change one of the characters in the curly brackets?
for ex.:
  1. char Line_One[3] = { ' ', ' ', ' ' };
turns into
  1. char Line_One[3] = { 'X', 'X', 'O' };

Thanks!
  1. char Line_One[3] = { 'X', 'X', 'O' };
This is the same as:
  1. char Line_One[3];
  2. Line_One[0] = 'X';
  3. Line_One[1] = 'X';
  4. Line_One[2] = 'O';

The 3 represents that you have an array of char called Line_One that contains three elements.
Last edited by VernonDozier; Jan 10th, 2009 at 10:24 pm. Reason: Fixed quote tag
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: variables with brackets??

 
0
  #3
Jan 10th, 2009
Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

  1. Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

  1. Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

  1. Line_one[3] = { 'X', ' ', 'O' }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: variables with brackets??

 
0
  #4
Jan 10th, 2009
Originally Posted by besktrap View Post
Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

  1. Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

  1. Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

  1. Line_one[3] = { 'X', ' ', 'O' }

Change the elements one at a time. To change the 0th element, do this:

  1. Line_one[0] = 'O';

Don't use the { and } again when you change the individual elements. Just use them in the beginning.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: variables with brackets??

 
0
  #5
Jan 10th, 2009
Originally Posted by besktrap View Post
Thanks! But how would I change the contents of variable with out completely erasing it?
ex:
In Tic Tac Toe, the third line looks like this:

  1. Line_one[3] = { ' ', ' ', ' ' }

Then the player puts and X on the first spot, so Line_one turns into:

  1. Line_one[3] = { 'X', ' ', ' ' }

then the computer puts an O on third spot:

  1. Line_one[3] = { 'X', ' ', 'O' }


first of all tick tac toe is a 2d array so define like this:
  1. char game[3][3] = {
  2. // 0 1 2
  3. ' ', ' ', ' ', // 0
  4. ' ', ' ', ' ', // 1
  5. ' ', ' ', ' ' // 2
  6. };
  7. // insert an x in the center:
  8. game[1][1] = 'X';
  9. // insert an o in the bottom right
  10. game[2][2] = 'O';
remember its oposite in programing so instead of [x][y] its actualy [y][x] so remember to switch them
instead of defining at 1,2 you need to do [2][1].
Also remember that its one less than you would think in you mind so you might want to make a function like so:

  1. void PlacePeice(char p,int x,int y)
  2. {
  3. game[--y][--x] = p; // [y - 1][x - 1] = peice;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: variables with brackets??

 
0
  #6
Jan 10th, 2009
Thanks again! But when I cout << game, i get

  1. 0x441000

What happend there?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: variables with brackets??

 
0
  #7
Jan 10th, 2009
Originally Posted by besktrap View Post
Thanks again! But when I cout << game, i get

  1. 0x441000

What happend there?
If you don't understand null terminators or pointers and dereferencing a pointer yet, it's best to do something like this instead:

  1. cout << game[1][2];

Replace 1 and 2 with any pair of numbers which are 0, 1, or 2. It will display a single character.

  1. cout << game[0];

will display up to a null terminator (which you haven't defined, so it'll display all 9 characters, then quite possibly garbage.

  1. cout << game;

will display an address in memory.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. char game[3][3];
  7.  
  8. game[0][0] = 'A';
  9. game[0][1] = 'B';
  10. game[0][2] = 'C';
  11. game[1][0] = 'D';
  12. game[1][1] = 'E';
  13. game[1][2] = 'F';
  14. game[2][0] = 'G';
  15. game[2][1] = 'H';
  16. game[2][2] = 'I';
  17.  
  18. // prints a character
  19. cout << game[0][0] << endl;
  20. // no null terminator, so will likely print garbage
  21. cout << game[0] <<endl; // prints 9 characters, then garbage
  22. cout << game[1] <<endl; // prints 6 characters, then garbage
  23. cout << game[2] <<endl; // prints 3 characters, then garbage
  24. // prints an address
  25. cout << game <<endl;
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC