For my homework assignment, I have to load a set of numbers into a 2-D Array. The first value in the file is the amount of rows + columns the array has (firstvalue = 2, then it's a 2 x 2 array). However, when I load the file into an array, it skips the first number and doesn't print it. How would go about saving the first value of the array and also print it within the array?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int firstvalue = 0;
if(infile.good())
{
infile >> firstvalue;
}
while(infile.good())
{
for (int rows = 0; rows < firstvalue; rows++)
{
for(int col = 0; col < firstvalue; col++)
{
infile >> a[rows][col];
}
}
}
The file should contain 2 2 3 4 5 in that case. The 2 is being taken and making it a 2 x 2 so that 3 4 5 is all that is left in the file. Since the 2x2 requires 4 numbers and the file now only has 3 it is taking 3 4 5 and the NULL after it to print 3 4 5 0.
Outside of that I'm not entirely sure on how to make it read the first number, flush it so it sees the whole file again.