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