Input and output problem with 2D array

This is a discussion on Input and output problem with 2D array within the C++ Programming forums, part of the General Programming Boards category; What's wrong with this? I thought the code doesn't have any problem but it just doesn't output what I want. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Unhappy 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.
    Last edited by Roy01; 10-22-2006 at 08:00 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    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
    Last edited by Roy01; 10-22-2006 at 08:27 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thx, but my problem is the input.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    So I just wasted my time to try to format input......I got it.Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21