I don't see any tests to verify that the files have been opened successfully. Do you know that the output file is actually being opened properly?
Example:
#include <iostream>
#include <fstream>
using std::cout;
using std::ofstream;
int main() {
ofstream outFile("filename.txt", std::ios::out | std::ios::trunc); //declare the output file stream
if (outFile.is_open()) //test for successful open
cout << "File opened successfully"; //report successful open
else {
cout << "Error opening file."; //report error
return 1;
}
for (int i = 0; i < 10; ++i) {
outFile << i << " "; //test output to file
}
return 0;
}
NOTE:
You don't generally need both parts of the test. I've only done it this way for demonstration purposes. Generally something like this is sufficient:
if (!outFile.is_open()) { //test for successful open
cout << "Error opening file."; //report error
return 1;
}
Notice the added "not" in the condition.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
Oh, I see what's going on. What's wrong with your attempt to output your matrix here:
for(row =0; row < NUMBER_OF_ROWS; row++)
{
outfile << board[row][col] << sum/col;
}
What's missing? You're outputting amatrix.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
No. The problem is that you only have 1 loop that controls row. You forgot to add an inner ("nested") loop to control col.
As a result, all of your output is bogus.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
for(row = 0; row < NUMBER_OF_ROWS; row++) {
for(col = 0; col < NUMBER_OF_COLUMNS; col++);
}
sumRows(board, NUMBER_OF_ROWS); //Line 4
cout << endl; //Line 5
Why did you do this to your input loop? This accomplishes absolutely nothing.
This version:
for(row = 0; row < NUMBER_OF_ROWS; row++)
for(col = 0; row < NUMBER_OF_COLUMNS; col++)
infile >> board[row][col];
printMatrix(board, NUMBER_OF_ROWS); //Line 2
cout << endl; //Line 3
sumRows(board, NUMBER_OF_ROWS); //Line 4
cout << endl;
was much better.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393