Hello momof4,
First off post the input file so everyone can see what yo are working with. This helps. It also helps to post the whole code because sometimes problems start in the code that is not shown. Like how is the array defined?
A question I have is the input file to contain numbers to setup a game board or does it contain all the numbers for a game or the numbers of a game played?
The for loops are designed to read 81 numbers. No more or no less. But the while loop will continue reading putting everything into one element until "eof" is reached, then the while loop fails and the for loops take over and start changing the "row" and "column" indices.
What you actually get from
fin >> board[r][c]
will depend on the C++ standards that you are compiling to.
There are several ways that you could change how the file is read, but first need to know what the file looks like.
You said:
it started adding in random numbers to fill out the array.
|
The program does not arbitrarily choose a random number to put into the file unless you tell it to by using something to generate a random number. Again this will depend on the C++ standards you are using.
Here are some suggestions for your code. These are not required, but can help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <fstream>
constexpr size_t MAXROW{ 9 };
constexpr size_t MAXCOL{ 9 };
bool readFile(const std::string fileName, int board[][MAXCOL])
{
// open the file
std::ifstream fin(fileName);
if (!fin)
{
std::cout << "Unable to open file " << fileName << '\n';
return false;
}
// read the file into the board
int count = 0;
for (int row = 0; row < MAXROW; row++)
for (int col = 0; col < MAXCOL; col++)
while (fin >> board[row][col])
count++;
if (count != 81)
{
std::cout << "Error: wrong amount of numbers in file. There are "
<< count << " numbers in the file.\n" << std::endl;
return false;
}
// close file
//fin.close(); // <--- Not necessary, but OK to leave.
return true;
}
|
Lines 3 and 4 are to replace the magic numbers in the rest of the function. It does not show as well, but these are global variables with file scope so they can be used anywhere.
In line 6 when passing a 1D or 2D array to a function the first dimension's size is not required, but OK if you put one there. The size of the second dimension is required.
Line 11 can be shortened to this. It works the same or maybe a little better than what you used.
Lines 20 - 22 are changed a bit. By using "row" and "col" instead of "r" and "c" just makes the code a little easier to read and understand. Using the constants to replace the magic numbers makes the code easier to maintain or change. These constants may not be as easy to see how they work as well as it would be in a different program where they would be used more often.
Closing the file stream may be good form, but it is not required. in a function when you leave the function the stream will close. Also if you are going to use a "close" statement you should put one after your line 21 to be proper. but still not necessary to do this.
Hope that helps,
Andy