Input and output problem with 2D array

• 10-22-2006
Roy01
Input and output problem with 2D array
What's wrong with this? I thought the code doesn't have any problem but it just doesn't output what I want.
Code:

```#include <iostream> using namespace std; void main() {         int a = 0, b = 0,size;         int array[10][10];                 cin>>size;         for(; a < size; a++)                 {             for(; b < size; b++)                                 {                                 cin>>array[a][b];//input entries                                 if ((b+1)%size==0)                                 cout<<endl;//end the line when a row is complete                                 }                 }         for(; a < size; a++)                 {             for(; b < size; b++)                                 {                                 cout<<array[a][b];//output entries in a row                                 if ((b+1)%size==0)                                 cout<<endl;//end the line when a row is complete                                 }                 } }```
If the size input is 2,then this is what I expected on the screen when input:
Code:

```2 1 2 3 4```
And expected output:
Code:

```1 2 3 4```
This is what I actually got:
Code:

```2 1 2 3 4```
And there is no output.:(
Plz help.
• 10-22-2006
anon
Code:

```        for(; a < size; a++)                 {             for(; b < size; b++)```
You don't give an initial value to a and b in these loops. By the time you get to the similar loops again, a and b both will be equal to size, and the loop will never start. (You have incremented both before, how can you expect them to be 0 again, without setting them to 0?)

Also, if user entered size bigger than 10, they would be in serious trouble.
• 10-22-2006
laserlight
Assuming you enter an integer in the range [0,10) for the size, the input should not be a problem. The user can just enter the input - extra whitespace (i.e. the new lines) does not matter.
• 10-22-2006
Roy01
I am not sure if I totally understood what you said.Sorry for my poor English.I modified it but still it is not exactly what I want.
New version:
Code:

```#include <iostream> using namespace std; void main() {         int a,b,size;         int array[10][10];                 cin>>size;         for(a=0; a < size; a++)                 {             for(b=0; b < size; b++)                                 {                                 cin>>array[a][b];//input entries                                 if ((b+1)%size==0)                                 cout<<endl;//end the line when a row is complete                                 }                 }         for(a=0; a < size; a++)                 {             for(b=0; b < size; b++)                                 {                                 cout<<array[a][b];//output entries in a row                                 if ((b+1)%size==0)                                 {                                         cout<<endl;//end the line when a row is complete                                 }                                 }                 } }```
actual input
Code:

```2 1 2 3 4```
output
Code:

```12 34```
• 10-22-2006
laserlight
Ah, that looks okay. Still, void main() should be int main(), you might as well declare and initialise the array loop indices in the loops, and your formatting looks off.
Code:

```#include <iostream> using namespace std; int main() {     int array[10][10];     int size;     cin >> size;     // assume size is in the range [0,10)     for (int a = 0; a < size; a++)     {         for (int b = 0; b < size; b++)         {             cin >> array[a][b]; //input entries         }     }     for (int a = 0; a < size; a++)     {         for (int b = 0; b < size; b++)         {             cout << array[a][b]; //output entries in a row             if ((b+1) % size == 0)             {                 cout << endl; //end the line when a row is complete             }         }     } }```
Of course, you need to add spaces to get the output that you want.
• 10-22-2006
anon
It seems you are trying to format input? That may not be so easy as to be worth the trouble.

As for outputting, output a space after each number you print.
• 10-22-2006
Roy01
Thx, but my problem is the input.
• 10-22-2006
laserlight
As far as I know, you cannot force input to be formatted in such a way using only standard C++. Of course, as a user you can format in the desired format.
• 10-22-2006
Roy01
So I just wasted my time to try to format input......I got it.Thank you so much!