3D array help please

I have a 3D array X[2][4][5] but what i want to do is read from a file an integer n_el that must be between 0<n_el<=40 and then read n_el integers into the array X.
Finally cout the array X
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
  #include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream IN("input.txt");
ofstream OUT("output");
if ( IN && OUT)
{
 int n_el;
 IN>>n_el;
 cout<<n_el;
  int X[2][4][5];
 for( int i=0; i<2; i++)
 {
        for(int j=0; j<4; j++)
        {
            for(int z=0; z<5; z++)
            {IN>> X[i][j][z];
             cout<<X[i][j][z]<<endl;
            }
        }
 }
}

else

    cout<<"error";
IN.close(); OUT.close();


}



Basically i don't know how to read n_el integers into the array.
How do i code it?
Thanks
Introduce another variable like int idx_el = 0;

Increase the index in the inner loop and break all loops if idx_el reached n_el
thanks!
Last edited on
Topic archived. No new replies allowed.