| | |
variables with brackets??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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.:
turns into
Thanks!
C++ Syntax (Toggle Plain Text)
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.:
C++ Syntax (Toggle Plain Text)
char Line_One[3] = { ' ', ' ', ' ' };
C++ Syntax (Toggle Plain Text)
char Line_One[3] = { 'X', 'X', 'O' };
Thanks!
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
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.:
turns intoC++ Syntax (Toggle Plain Text)
char Line_One[3] = { ' ', ' ', ' ' };
C++ Syntax (Toggle Plain Text)
char Line_One[3] = { 'X', 'X', 'O' };
Thanks!
C++ Syntax (Toggle Plain Text)
char Line_One[3]; Line_One[0] = 'X'; Line_One[1] = 'X'; 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
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:
Then the player puts and X on the first spot, so Line_one turns into:
then the computer puts an O on third spot:
ex:
In Tic Tac Toe, the third line looks like this:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { ' ', ' ', ' ' }
Then the player puts and X on the first spot, so Line_one turns into:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', ' ' }
then the computer puts an O on third spot:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', 'O' }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { ' ', ' ', ' ' }
Then the player puts and X on the first spot, so Line_one turns into:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', ' ' }
then the computer puts an O on third spot:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', 'O' }
Change the elements one at a time. To change the 0th element, do this:
C++ Syntax (Toggle Plain Text)
Line_one[0] = 'O';
Don't use the { and } again when you change the individual elements. Just use them in the beginning.
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { ' ', ' ', ' ' }
Then the player puts and X on the first spot, so Line_one turns into:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', ' ' }
then the computer puts an O on third spot:
C++ Syntax (Toggle Plain Text)
Line_one[3] = { 'X', ' ', 'O' }
first of all tick tac toe is a 2d array so define like this:
C++ Syntax (Toggle Plain Text)
char game[3][3] = { // 0 1 2 ' ', ' ', ' ', // 0 ' ', ' ', ' ', // 1 ' ', ' ', ' ' // 2 }; // insert an x in the center: game[1][1] = 'X'; // insert an o in the bottom right game[2][2] = 'O';
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:
C++ Syntax (Toggle Plain Text)
void PlacePeice(char p,int x,int y) { game[--y][--x] = p; // [y - 1][x - 1] = peice; }
Thanks again! But when I cout << game, i get
What happend there?
C++ Syntax (Toggle Plain Text)
0x441000
What happend there?
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
Thanks again! But when I cout << game, i get
C++ Syntax (Toggle Plain Text)
0x441000
What happend there?
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
cout << game;
will display an address in memory.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { char game[3][3]; game[0][0] = 'A'; game[0][1] = 'B'; game[0][2] = 'C'; game[1][0] = 'D'; game[1][1] = 'E'; game[1][2] = 'F'; game[2][0] = 'G'; game[2][1] = 'H'; game[2][2] = 'I'; // prints a character cout << game[0][0] << endl; // no null terminator, so will likely print garbage cout << game[0] <<endl; // prints 9 characters, then garbage cout << game[1] <<endl; // prints 6 characters, then garbage cout << game[2] <<endl; // prints 3 characters, then garbage // prints an address cout << game <<endl; return 0; }
![]() |
Similar Threads
- Student Needs Homework Help! (Computer Science)
- url variables are killing me (ColdFusion)
- tuff question in Java (Java)
- Using php variables inside an array (PHP)
- PHP Variables (PHP)
- can't concatenate variables and add text (ASP.NET)
- Arrays (C++)
- Objects (C)
- capturing text (C)
Other Threads in the C++ Forum
- Previous Thread: Calling c++
- Next Thread: Beep on Events (MSVC++ 2005)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






