Code error (char variable problem)

hello,

I have a problem. I am writing some code for game tic tac toe(at least I think that's its name in English) and this code gives me error, when I try to use variable 'z' in object named "grid" (error: invalid types 'char[int]' for array subscript)(in lines 54 and 57) if someone can tell me, what I am doing wrong with char, I would really appreciate it. if you need to explain something what i did not mentioned just tell me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

class myclass{

public:
   void grid(char a[2],bool b){
      string a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,c1,c2,c3,c4,c5,d1,d2,d3,d4,d5,e1,e2,e3,e4,e5;

              if ( a[2]=='32'){
        a1=a2=a3=a4=a5=b1=b2=b3=b4=b5=c1=c2=c3=c4=c5=d1=d2=d3=d4=d5=e1=e2=e3=e4=e5='?';
              }
              switch (a[2]) {
              case 'a1':
                  a1='X';
                  break;
              }

    cout <<"\n    _______________________\n"<<"    II a I b I c I d I e II\n"<<"    -----------------------"<<endl;
                                     cout <<" 1: II "<<a1<<" I "<<b1<<" I "<<c1<<" I "<<d1<<" I "<<e1<<" II"<<endl;
    cout <<"    _______________________\n"<<" 2: II "<<a2<<" I "<<b2<<" I "<<c2<<" I "<<d2<<" I "<<e2<<" II"<<endl;
    cout <<"    _______________________\n"<<" 3: II "<<a3<<" I "<<b3<<" I "<<c3<<" I "<<d3<<" I "<<e3<<" II"<<endl;
    cout <<"    _______________________\n"<<" 4: II "<<a4<<" I "<<b4<<" I "<<c4<<" I "<<d4<<" I "<<e4<<" II"<<endl;
    cout <<"    _______________________\n"<<" 5: II "<<a5<<" I "<<b5<<" I "<<c5<<" I "<<d5<<" I "<<e5<<" II\n"<<"    _______________________\n"<<endl;
    }
};

int main() {
    myclass get;
   srand(time(0));
   bool y;
   string p1,p2;
   int x;
   x =1+(rand() %6);
   char z[2];
   z[2]='32';

    cout << "Welcome to 'tic tac toe' multiplayer game\nnow insert player names\nPlayer 1:";
   cin >> p1;
   cout <<"Player 2:";
   cin >> p2;

   if (x<4){
    cout <<p1<<" is first"<<endl;
    y=true;
   }
    else {
    cout <<p2<<" is first"<<endl;
    y=false;
    }
    get.grid(z[2],y);
    cout << "type in letter and number of square, where you want place your symbol"<<endl;
    cin >>z;
    get.grid(z[2],y);
    cout <<z<<endl;

    return 0;
}
Last edited on
Line 13: You're using the assignment (=) operator, not the comparison operator (==).

Line 13,16: You've declared char a[2] which is an array of 2 characters. You're trying to reference the third character, which is going to cause an out of bounds reference.

Line 13,39: You're trying to store a 16 bit literal into an 8 bit character.

Line 54,57: You' re trying to subscript a simple character (not an array).

It looks like you need to look up the difference between arrays of characters and single char variables. You're using 'z' a single char like it is an array of char.

Some of your other problems are that you are trying to use multiple characters in a character constant. A character constant should consist of only one character 'Y'.

You also need to learn the difference between the comparison operator== and the assignment operator=.

You don't seem to understand the difference between your variable names and the values these variables contain. In this snippet: case 'a1':, you have a character constant with two characters (a character constant is denoted by the single quotes), but it really looks like you want to use the contents of the variable not a character constant.

I suggest you start by thoroughly reading this link,
http://www.cplusplus.com/doc/tutorial/

You have seemed to either gloss over the very beginning portions of your book that discusses variables, string and character constants and many other very basic tenets of the language or forgotten the basics.

Line 13,16: where do I refer to third char?

This question has many different problems that need answering.
1
2
3
4
5
6
7
8
9
10
11
...
int main() {
...
   char z[2]; 
   z[2]='32';
...
    get.grid(z[2],y);
...   
void grid(char a[2],bool b){
...
             if ( a[2]=='32'){


First you seem to have "forgot" that arrays start at zero and stop at size - 1. So when you define a variable with a size of 2 char z[2] you only have two elements, 0 and 1, your trying to access element 3 in several places z[2]='32';, you're also using a multi-character char constant '32' which will not do what you want.

Next you're sending a single character to a function that expects an array get.grid(z[2],y);, worse yet you're accessing the array out of bounds because your trying to send the third character of an array that only contains two elements. If you want to pass the array you don't need the braces or the size.

I really suggest you start reading that tutorial I provided in my last post, probably the first 10 or so links may help bring you up to speed.



Topic archived. No new replies allowed.