Strange output

I just wanted to make a program which takes input from the user, puts it in a character array and then just prints the co-ordinates where there are Xs, pretty simple really. It didn't work, so I wrote the following code to see if it reads the array correctly, 'cause you never know. But it outputs some strange results.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int x, y;
cin>>x>>y;
char pin[x][y];
for (int i=0; i<x; i++){
    for (int j=0; j<y; j++)
     cin>>pin[x][y];
}

for (int q=0; q<x; q++){
    for(int w=0; w<y; w++){
        /*if(pin[q][w] == 'X'){
         cout<<"("<<q<<","<<w<<")"<<endl;
        }*/
        cout<<pin[q][w]<<" ";
    }
    cout<<endl;
}


And here is a screen shot with what it outputs. I really don't understand why.
http://i.imgur.com/kudF0Dh.png
Last edited on
closed account (48T7M4Gy)
const int x = 3, y = 4; for your line 1

3 and 4 are just examples of what u need

Last edited on
@kemort
Constant? How can I input from keyboard with a constant variable?
closed account (48T7M4Gy)
const has the meaning read-only and in this case this is what you have to do to declare an array if you know the size of the array. If you don't know the size and that is input at run time you have a dynamic array which requires new/delete keywords and the use of pointers. I don't know whether you have reached that point in your studies.
Line 6 is incorrect. Try i and j instead.
cin>>pin[x][y];
Last edited on
closed account (48T7M4Gy)
there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new and delete.


http://www.cplusplus.com/doc/tutorial/dynamic/
Thanks for all the help guys!
Topic archived. No new replies allowed.