Hey guys!

I must have some sort of logic problem that I'm just not seeing. I just want to read a file into a 2D array. The dimensions of the array are given by the very first line of the input file. Everything works hunky dorey except that when I print out the contents of my 2D array, I've got some extra zeroes at the end.

Here's the code:

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

int main()
{
     ifstream infile;    //declare a file variable
     int dim;
     
    
     infile.open("input.dat");     //open a file
     assert(infile);               //make sure file opened
     

     
     infile>>dim;
     
     int matrix[dim][dim];
     
     for(int row=0;row<dim;row++)
     {
      for(int col=0;col<dim;col++)
      {
       matrix[row][col]=0;
       }
     }
     
     while(infile)
     {
     for (int row=0;row<dim;row++)
      {
          for (int col=0;col<dim;col++)
          {
           infile>>matrix[row][col];
           }
      }
     infile.close();
     }
//print 2D array
for(int row=0;row<dim;row++)
{
        for(int col=0;col<dim;col++)
        {
                cout<<matrix[row][col];
                cout<<endl;
        }
}
    system("pause");
    return 0;
    
}

Here's my output:
123
456
789
0
0
0
0
0
0
Those zeroes at the end shouldn't be there. It's been awhile since I worked with arrays so I must be overlooking something...

This is what is in my input file:
3
123
456
789

Thanks for any help!

Recommended Answers

All 7 Replies

I assume that you realized that the zeros printed out are the same zeros that you stuff the array with in your first for-loop.

This is because you set the dimension to 3. That creates a matrix sized 3*3, giving you 9 stored values. As you can see your output also consists of 9 values.

If you want a very simple solution for printing out an odd set of numbers just do like you did and let the 0 be a forbidden number:

for(int row=0;row<dim;row++)
{
        for(int col=0;col<dim;col++)
        {
            if(matrix[row][col] != 0)
                cout << matrix[row][col] << endl;
        }
}

Okay, that "covers up" the problem but what I don't understand is the logic behind why it doesn't work the way I did it. My matrix should be 3*3 and nothing more.

Thanks for the suggestion though!

If you want a very simple solution for printing out an odd set of numbers just do like you did and let the 0 be a forbidden number:

for(int row=0;row<dim;row++)
{
        for(int col=0;col<dim;col++)
        {
            if(matrix[row][col] != 0)
                cout << matrix[row][col] << endl;
        }
}

Looks to me like your data input file is incorrect.

3
123
456
789

Note the lack of spaces between the digits. I assume you want this:

3
1 2 3
4 5 6
7 8 9

Note the spaces. The >> operator treats the spaces as delimiters. Without them, It sees a line with "123", and assumes that's one number, not three, and that it is 123 (One-hundred-twenty-three).

If the input file is indeed correct the way it is (i.e. no spaces, intentionally), then you could write a program that would treat each digit as a separate integer, but you would likely not use the >> operator.

when I stick spaces in between the digits and run your program, I get:

1
2
3
4
5
6
7
8
9

Ah, you're very right. My input file was incorrect. In addition, I see that I've done something else wrong because I want it to print out in matrix form (3*3) - basically the same way it looks in the input file.
Thanks VernonDozier!

Looks to me like your data input file is incorrect.

3
123
456
789

Note the lack of spaces between the digits. I assume you want this:

3
1 2 3
4 5 6
7 8 9

Note the spaces. The >> operator treats the spaces as delimiters. Without them, It sees a line with "123", and assumes that's one number, not three, and that it is 123 (One-hundred-twenty-three).

If the input file is indeed correct the way it is (i.e. no spaces, intentionally), then you could write a program that would treat each digit as a separate integer, but you would likely not use the >> operator.

when I stick spaces in between the digits and run your program, I get:

1
2
3
4
5
6
7
8
9

Ah, you're very right. My input file was incorrect. In addition, I see that I've done something else wrong because I want it to print out in matrix form (3*3) - basically the same way it looks in the input file.
Thanks VernonDozier!

You're welcome. Adding a few comments might clarify the process. Here's your output loop, unchanged with comments added.

//print 2D array
for(int row=0;row<dim;row++)
{
        // display a single row
        for(int col=0;col<dim;col++)
        {                
                cout<<matrix[row][col];  // display a particular number within a row
                cout<<endl;  // Done with displaying the full row.  Display an endline.
        }
}

The comments are what you WANT to do. Look at the code and placement and make sure that it's what you really ARE doing. Change the code so that the code reflects the comments.

Ha, thanks! Got it now! Amazing how those tricky little for loops work. lol! Just had to move that endl...

You're welcome. Adding a few comments might clarify the process. Here's your output loop, unchanged with comments added.

//print 2D array
for(int row=0;row<dim;row++)
{
        // display a single row
        for(int col=0;col<dim;col++)
        {                
                cout<<matrix[row][col];  // display a particular number within a row
                cout<<endl;  // Done with displaying the full row.  Display an endline.
        }
}

The comments are what you WANT to do. Look at the code and placement and make sure that it's what you really ARE doing. Change the code so that the code reflects the comments.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.