Program doesn't work properly

I don't know why the program stops after line 27, it doesn't read ch or anything else after the first array

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
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream IN("input.txt");
ofstream OUT("output");
int X[2][4][5];
if ( IN && OUT)
{
 int n_el, idx_el=1;
 IN>>n_el;
 for( int i=0; i<2; i++)
 {
    for(int j=0; j<4; j++)
        {
            for(int z=0; z<5; z++)
            {
               while(idx_el<n_el)
               {
                IN>> X[i][j][z];
                cout<<X[i][j][z]<<endl;
                idx_el++;
               }
            }
        }
 }
                char ch;
                int x;
                IN>> ch;
                cout<<ch;
                    if(ch=='H')
                    { IN>>x;
                            for(int i=0;i<2;i++)
                            {
                                for(int z=0;z<5;z++)
                                {
                                    while(idx_el<n_el)
                                    {
                                    OUT<<X[i][x][z]<<" ";
                                    idx_el++;
                                    }
                                }
                            OUT<<"  "<<endl;
                            }

                    }
                        if(ch=='V')
                        {int y;
                         IN>>y;

                                    for(int i=0;i<2;i++)
                                    {
                                        for(int j=0;j<4;j++)
                                        {
                                        OUT<<X[i][j][y]<<" ";
                                        }
                                        OUT<<"  "<<endl;
                                    }


                        }


}

else

    cout<<"errore con i files";
IN.close(); OUT.close();


}
Last edited on
I do suspect that the content of IN plays a part in the problem.

Furthermore, could you edit the line 22 to perhaps lead to an another relevation?
1
2
cout << "i=" << i << " j=" << j << " z=" << z
     << " idx_el=" << idx_el << " X=" << X[i][j][z] << endl;

(What your code does now may be intentional, but it looks eerie to me.)
what's the content of input file?
because what I am trying to do is:
In the file input i can write this for example:
30
1 2 1 0 0 0 2 2 3 1
0 0 1 1 1 1 1 2 1 0
1 1 0 1 0 0 2 2 2 0
1 2 2 2 2 2 1 1 2 1

H 1

So my array will be filled with the first 30 values.
After that it should read the char H and proceed. But it doesn't and I don't know why.
To me it does look quite obvious that 1 != 'H' and 1 != 'V'.
Why? There are 40 integers between the 30 and the H.

PS. I do urge you to try the debug statement that I wrote in your program with your input.

PS2. You don't validate the integer that you read after the character. You should, because you do use it as array index and you don't want to get out-of-range errors. (Do you?)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 for( int i=0; i<2; i++)
 {
    for(int j=0; j<4; j++)
        {
            for(int z=0; z<5; z++)
            {
               while(idx_el<n_el)
               {
                IN>> X[i][j][z];
                cout<<X[i][j][z]<<" ";
                idx_el++;
               }
            }
        }
 }


in this part, you are inputting all values to X[0][0][0] !!!

so, all other X subscripts has undefined value.
i.e. add cout << "\n" << X[0][0][1] << "\n"; after line 27.
it wont print "2" (2nd element)
Last edited on
i see.. maybe it's better to use while instead than for, isn't it?

At the moment I have no clues of how to do it anymore =(

This is a piece of the new code.
It seems to be working.
And my input is like this:
21
1 2 1 0 0 0 2 2 3 1
0 0 1 1 1 1 1 2 1 0
1
H 0

so that After I write it down and I declare H 3 my output should be

11210 and 10000 because of the Array[0][0][z] and [1][0][z]

but I only want to write in the output 11210 and 1, but not the empty one. How do I do that?

I also want if I declare in input example :

15
1 2 1 0 0 0 2 2 3 1
0 0 1 1 1
H 3
i want in output "the array is empty" because X[0][3][z] and X[1][][] have no values..

I have no clues of how to write it down..


But I have a question. How do I write if the array
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


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char ch;
    int p;
 	int n_el=0;
 	int i=0;
 	int k=0;
 	int j=0;
 	int z=0;
 	int A[2][4][5];
 	ifstream IN("input");
	ofstream OUT("output");
	IN>>n_el;
    while(i<n_el)
 	{
    	IN>>A[k][j][z];
    	cout<<A[k][j][z]<<" ";
    	z++;
    	if(z==5)
    	{
       		z=0;
       		j++;
       		cout<<"\n";
	   	}
	   	if(j==4)
	   	{	
	   		j=0;
	   		k++;
	   	}
	   	i++;
 	}


why do you need 3d array?
because my teacher wants so..
Could you explain the logic?

On one hand you have a 40-element array (formally 3D 2*4*5).
On the other hand you have uncertain amount of elements in the file; could be less or even more than 40.

Why?


It would be more intuitive to have a container for exactly the amount of values that the input has. If that data must have spesific dimensionality, then some exact rules must state it (either implicitly or explicitly with additional input data).

Anyway, you do have two pieces of data: n_el and A. You can calculate from n_el and the fixed dimensions of A whether element (k,j,z) has value or is nil. Most multi-dimensional array texts should describe how to convert between 1D index and nD index.
Topic archived. No new replies allowed.