Hi, I'm trying to get this program to read a file which contains a matrix.
I am trying to output a text file which has the matrix of the input file but with the average of each row. Every time I enter the name of the inputFile and the outputFile I get an outputFile with nothing on it. Can someone take a look at what I'm doing wrong here? Thanks in advanced, bookmark.

#include <iostream> 
#include <iomanip>	 
#include <fstream>
   
using namespace std;

const int NUMBER_OF_ROWS = 9;
const int NUMBER_OF_COLUMNS = 9;

void printMatrix(int matrix[][NUMBER_OF_COLUMNS], 
                 int NUMBER_OF_ROWS);
void sumRows(int matrix[][NUMBER_OF_COLUMNS], 
             int NUMBER_OF_ROWS);

int main()
{
ifstream infile;
ofstream outfile;

char inputFile[51];
char outputFile[51];
	
cout << "Enter the input file name: ";
cin >> inputFile;
cout << endl;

infile.open(inputFile);

cout << "Enter the output file name: ";
cin >> outputFile;
cout << endl;

outfile.open(outputFile);

int sum;
int col;
int row;

int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; 
                        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;                                   //Line 5

	for(row =0; row < NUMBER_OF_ROWS; row++)
	{
outfile << board[row][col] << sum/col;
	}
    return 0;
}

void printMatrix(int matrix[][NUMBER_OF_COLUMNS], 
                 int noOfRows)
{
    int row, col;

    for (row = 0; row < noOfRows; row++)
    {
        for (col = 0; col < NUMBER_OF_COLUMNS; col++)
            cout << setw(5) << matrix[row][col] << " ";

        cout << endl;
    }
}

void sumRows(int matrix[][NUMBER_OF_COLUMNS], int noOfRows)
{
    int row, col;
    int sum;

         //Sum of each individual row
    for (row = 0; row < noOfRows; row++)
    {
        sum = 0;

        for (col = 0; col < NUMBER_OF_COLUMNS; col++)
            sum = sum + matrix[row][col];

        cout << "Sum of row " << (row + 1) << " = " << sum
             << endl;
    }
	system("pause");
}

Recommended Answers

All 10 Replies

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.

When I run the program I input the input file which contains the matrix. Then I create an output file when the program prompts me. When I check to see if the output file is in the folder, I see it there but there is nothing in it. It's just blank. How do I test if it's being opened properly? thanks for your help.

Ahh ok. I'll add that. thanks again.

I have tested it but it shows that the output file is being opened.

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 a matrix.

So my only problem is that board should be matrix?

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.

Ahh. thanks I didn't get what you were saying lol.

Ok. So now what I have is this code.

#include <iostream> 
#include <iomanip>	 
#include <fstream>
   
using namespace std;

const int NUMBER_OF_ROWS = 4;
const int NUMBER_OF_COLUMNS = 20;

void printMatrix(int matrix[][NUMBER_OF_COLUMNS], 
                 int NUMBER_OF_ROWS);
void sumRows(int matrix[][NUMBER_OF_COLUMNS], 
             int NUMBER_OF_ROWS);

int main()
{
ifstream infile;
ofstream outfile;

char inputFile[51];
char outputFile[51];
	
cout << "Enter the input file name: ";
cin >> inputFile;
cout << endl;

infile.open(inputFile);

cout << "Enter the output file name: ";
cin >> outputFile;
cout << endl;

outfile.open(outputFile);

if(!outfile.is_open()){ 
	cerr << "ERROR IN OPENING FILE" << endl;
	exit(EXIT_FAILURE);
}

int sum;
int col;
int row;

int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; 
					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

	for(row =0; row < NUMBER_OF_ROWS; row++)
		{
		for(col=0; col < NUMBER_OF_COLUMNS; col++)
	outfile << board[row][col];
	}
    return 0;
}
void sumRows(int matrix[][NUMBER_OF_COLUMNS], int noOfRows)
{
    int row, col;
    int sum;

         //Sum of each individual row
    for (row = 0; row < noOfRows; row++)
    {
        sum = 0;

        for (col = 0; col < NUMBER_OF_COLUMNS; col++)
            sum = sum + matrix[row][col];

    }
	system("pause");
}

I'm getting an output on my outputfile but it is giving me a lot of repeated number such as this

"-858993460-858993460-858993460-858993460"

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.