[Error] invalid types 'int[int]' for array subscript

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
#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
	inFile.open ("out.txt");
  while(!inFile.eof()) {
    inFile>>n;
    if(n<0){
    	n;
    }
}
cout<<"\n\n";
decryption(n);
system("pause");  
}
int decryption(int a){
	int b[2][2]={{1,a},{0,1}};
	int m;
ifstream inFile;
inFile.open ("out.txt");
  while(!inFile.eof()) {
    inFile>>m;
    if(m<0)
    break;	
    
    //cout <<m<<"\t";
    for(int i;i<2;i++){
    	for(int j;j<2;j++){
    	cout<<m[i][j]<<"\t";
    	}
    }
    //multiply(m,b);
}
}

hi,

I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<"\t";

could you help me please?!
Last edited on
What are you doing on line 33? m is a single integer, not a 2D array of integers.
I am trying to store m into 2D array
I am trying to store m into 2D array


Your 2D array is 'b'.
So if you want to store 'm' into 'b', you need to do an assignment:

 
b[i][j] = m;


'cout' will just output something to the console - it will not assign anything.


Also... you need to initialize your loop counters on lines 31 and 32 to zero, otherwise they will start with random garbage. Remember that C++ does not automatically zero new variables for you:

1
2
3
4
for(int i;i<2;i++){
//      ^
//      |
//   Set this to zero! 



EDIT:

Also... line 13 does nothing.... which makes that whole if block pointless:

1
2
3
    if(n<0){
    	n;  // ???  what are you trying to do here?
    }
Last edited on
thanks. my mistake I didn't notice that on line 31 and 32 and it was not giving error when I compile it. what I am aiming to do is to multiply (m) after assigning it to 2D array by (b) matrix in order to decrypt a text file. but the problem is when I assign (m) to z[i][j], and write it inside two for loops, like this:
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
z[i][j]=m;
cout<<z[i][j]<<"\t";
}
}
it repeats the numbers inside the text file 4 times for each number.
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
#include <iostream>
#include<fstream>
int decryption(int);
using namespace std;
main(){
int n;
ifstream inFile;
	inFile.open ("out.txt");
  while(!inFile.eof()) {
    inFile>>n;
    if(n<0){
    	n;
    }
}
cout<<"\n\n";
decryption(n);
system("pause");  
}
int decryption(int a){
	int b[2][2]={{1,a},{0,1}};
	int m;
	int z[2][2];
ifstream inFile;
inFile.open ("out.txt");
  while(!inFile.eof()) {
    inFile>>m;
    if(m<0){
    	break;
    }
    	
    for(int i=0;i<2;i++){
    	for(int j=0;j<2;j++){
    		z[i][j]=m;
    		cout<<z[i][j]<<"\t";
    	}
    }
    	
    }
       
}


here are the codes again after modification. thanks for help.I appreciate it.
note: I haven't finished the compilation yet.I will multiply (b) by (z) after handling the repeatition issue explained above.
Last edited on
Topic archived. No new replies allowed.