Strings

Hey this is my class:
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
class boys
{
private:
      string h2bac[50][2];
      string h4bac[100][4];
      string h6bac[80][6];
      string h2bnac[50][2];
      string h4bnac[100][4];
      string h6bnac[80][6];
      
public:
      static int nb3;
      boys(){
      int i,j,k;
      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bac[i][j]="";
      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bac[100][4]="";
      for(i=0;i<80;i++)
      for(j=0;j<6;j++)
      h6bac[80][6]="";
      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bnac[50][2]="";
      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bnac[100][4]="";
      string h6bnac[80][6];};
      static void addblocksboys(unsigned int &x);
      int roomallocate(char s[]);
      void changeblock(int &z,unsigned int x);
      
      friend void checkavailboys();
}*p,a[6];

This is one of the functions..........
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
void checkavailboys(int &x)
      {
             
             int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l,m,n,o,p;
             cout<<"The no of rooms which are not allocated are:\n";
             for(i=0;i<6;i++)
             {
                             for(j=0;j<50;j++)
                             for(k=0;k<2;j++)
                             {if((a[i].h2bac[j][k]).compare("")==0)
                                                            a++;
                              if((a[i].h2bnac[j][k]).compare("")==0)
                                                             b++;
                              }
                             for(l=0;l<100;j++)
                             for(m=0;m<4;j++)
                             {if((a[i].h4bac[j][k]).compare("")==0)
                                                            c++;
                              if((a[i].h4bnac[j][k]).compare("")==0)
                                                             d++;
                              }
                             for(n=0;n<80;j++)
                             for(o=0;o<6;j++)
                             {
                             if((a[i].h6bac[j][k]).compare("")==0)
                                                            e++;
                              if((a[i].h6bnac[j][k]).compare("")==0)
                                                             f++;
                              }
cout<<"Block no:"<<i<<"2bed ac:"<<a<<endl<<"2bed non ac:"<<b<<endl<<"4bed ac:"<<c<<endl<<"4bed non ac:"<<d<<endl<<"6bed ac:"<<e<<endl<<"6bed non ac:"<<f<<endl;
             }
}

I am getting an error in this program... could someone help me... please
Thanks in advance...
Could you be a little more clever and report what error you got.
Could you be a little more clever and report what error you got.

What, you mean you can't just telepathically read it from his mind, like the rest of us obviously can?

:P
This code gave me cancer.

What happened to h6bnacs there?
There is no any sense in your code. Let consider for example the class constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
      boys(){
      int i,j,k;

      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bac[i][j]="";

      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bac[100][4]="";

      for(i=0;i<80;i++)
      for(j=0;j<6;j++)
      h6bac[80][6]="";

      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bnac[50][2]="";

      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bnac[100][4]="";

      string h6bnac[80][6];};


First of all all elements of these arrays that have type std::string are initialized
by default to an empty string. So for example this loop

1
2
3
      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bac[i][j]="";


has no any sense. Moreover in the very end of the constructor you defined one more local array

string h6bnac[80][6];

that is not used and will be deleted at once after the constructor finishes its work.
Sorry sorry guys....Mikey boy ... vvery funny .. I would have been glad if you would have helped...
actually my code for constructor is incomplete......
I will post the code for the constructor again...
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
 boys(){
      int i,j,k;

      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bac[i][j]="";

      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bac[100][4]="";

      for(i=0;i<80;i++)
      for(j=0;j<6;j++)
      h6bac[80][6]="";

      for(i=0;i<50;i++)
      for(j=0;j<2;j++)
      h2bnac[50][2]="";

      for(i=0;i<100;i++)
      for(j=0;j<4;j++)
      h4bnac[100][4]="";

      for(i=0;i<800;i++)
      for(j=0;j<6;j++)
      string h6bnac[80][6];}

Actually.. Here I am initialising all the strings to ""
Well the errors are coming in the compare function that i am using checkavailboys function..
I am using Dev C++ 4.9.9.0 as a compiler ... and I am getting the following error
invalid types `int[int]' for array subscript
for all the 6 time I am using the compare function..
Where have I gone wrong??
Did you read my previous post? Your code has no any sense.
You should pay attention to the advice people have already given you. Vlad was correct in what he said. You don't need to initialise those strings to "" as the string constructor already does it for you. And your h6bnac array drops out of scope as soon as it is created, so will be destroyed again immediately.

The problem your compiler is complaining about is where, for example, you have a[i]. a is an integer, so why are you trying to use it like a pointer/array?
Okkk thanks mikey boy and vlad I got the error solved
You're welcome :)
Topic archived. No new replies allowed.