-1

I am trying to add elements to my file using a method I have already used and was proven to be successful, however now when I do it I get the numbers I want as well as a bunch of other numbers that aren't in my file and don't make any sense

const int MAX_SIZE = 21;

int readSquare(int square[MAX_SIZE][MAX_SIZE], string inputFileName){ //reads file into an array

    int value;

    ifstream inFile;
    inFile.open(inputFileName);

    if (inFile) //if the input file to be read open successfully then goes on
    {
        int temp;
        inFile >> temp;

        if (temp>21) {
            temp=21;
        }
        for (int i = 0; i < MAX_SIZE; i++)
        {
            for(int j = 0; j < MAX_SIZE; j++)
            {
                inFile >> square[i][j];
            }
        }

    } else {
        inFile.close();
        return 0; //returns 0 if couldnt open file
    }
    inFile.close();

    cout << "Magic square" << endl;
    for(int i=0;i<MAX_SIZE;i++)
    {
        for(int j=0;j<MAX_SIZE;j++)
        {
            cout << square[i][j] << " ";
        }
        cout<<endl;
    }
    return 1;
}

This is the file I am using on my code

3
4 9 2
3 5 7
8 1 6

And this is the result I get(goes on for a while but I only took the top portion)

4 9 2 3 5 7 8 1 6 16840768 6619136 6643024 23198772 0 1942212500 127 917504 6643024 786434 6643032 0
65536 30 0 31 0 13930549 30 593 6619744 6619744 -2 127 46 6420808 1997546816 -1759127226 -2 6420704 1997359545 4096 4104
0 6420680 6634144 6619136 6421232 4104 6619744 0 3 0 4096 6420732 1997535944 6420804 655612 655360 2 9 0 2 6420976
0 1997378284 6420976 663276 1952 229640288 663200 655360 0 1997377793 6421060 661336 9 16777596 0 13080 236 661336 2 16777596 -530786634
Unknownzdx
  • 175
  • 2
  • 11

1 Answers1

1

Hat tip to @melpomene for working through the details in the main comments.

Op, you're iterating over the entire range of the array regardless of the availability of input data. I suggest you do the following so the results are less random in appearance:

  • initialize the values in the 2D array to zero.
  • limit the input samples to the quantity you're expecting, and not to exceed the size of the array in either dimension.

In your post, you're showing the value of 3 in the first line of the input file. What does that mean -- 3 lines, 3 samples, or 3 samples for each of the 3 lines?

Since the input file has 3 samples per line, I'm guessing the initial value in the data file represents the samples per line where the values for each line are assigned to an individual inner array.

Without deviating too much from your post, consider the following:

// clear the array for easier diags
for (int n = 0; n < MAX_SIZE; n++)
  for (int m = 0; m < MAX_SIZE; m++)
    square[n][m] = 0;

int cols;

inFile >> cols; // first line of data file indicating the samples in each row

if (cols > MAX_SIZE) // don't exceed the size of the inner array
  cols = MAX_SIZE;

for (int i = 0; i < MAX_SIZE; i++)
{
    for(int j = 0; j < cols; j++)
    {
        if (!(inFile >> square[i][j])) //  read until EOF
        {
          i = MAX_SIZE; // force outer loop to terminate since break only affects the inner loop.
          break;
        }
    }
}

See How does ifstream's eof() work?

bvj
  • 2,846
  • 27
  • 26