0

I am having trouble reading the second matrix from my file. Can anyone help me please? Thank you!

Attachments
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{   
	ifstream in;
    ofstream out;
	int num1, num2, num3;
	
	
	int i = 0;
	int counter = 0;
   
	vector < vector <int> > MatrixA;
    vector < int > RowA; 
	
	vector < vector <int> > MatrixB;
    vector < int > RowB; 
    
	in.open ( "j:\\input1.txt" );

	if ( !in )
	{
	cout << "Input file ( input1.txt ) does not exit." << endl;
	return 1;
	system ( "pause" );
	}

	
	while (in)
	{
	
	for (i =0; i<9; i++){

	in >> num1 >> num2 >> num3;
	{
    RowA.push_back(num1);
	RowA.push_back(num2);
	RowA.push_back(num3);
    MatrixA.push_back(RowA);
	RowA.clear();
	
	}
	
}
	
	for (i=0; i < 9; i++){

    in >> num1 >> num2 >> num3;
	{
    RowB.push_back(num1);
	RowB.push_back(num2);
	RowB.push_back(num3);
    MatrixB.push_back(RowB);
	RowB.clear();
	}
 }
}
		
	
    for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
    cout << MatrixA[i][j]; 
    }
    cout << endl;
}

    for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
    cout << MatrixB[i][j]; 
    }
	cout << endl;
}


   cout << endl; 
   cout << "Vector size: " << MatrixA.size() << endl << endl;
   cout << "Vector size: " << MatrixB.size() << endl << endl;
	
	//outfile.open ( "j:\\output.txt" );
  	cout << "Processing data......." << endl;
    system ("pause");

	

	in.close();
	out.close();
    return 0;
}
3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by vrga
0

What does the sample data in your file look like ?

Regarding your code, you need to be careful about the loop which is reading from your file. Unless your total number of records in the file is a multiple of 9 you will not read the values into the vectors correctly. Its likely you will keep looping for values even after you reach eof ..

0

Just how big are your matrices supposed to be? What is the input data. From your code, I see two sizes:

for (i =0; i<9; i++)
       {

	in >> num1 >> num2 >> num3;
	{
          RowA.push_back(num1);
	  RowA.push_back(num2);
	  RowA.push_back(num3);
          MatrixA.push_back(RowA);
	   RowA.clear();
	
	}
	
      }
	
		
	
    for(int i=0; i<3; i++)
   {
       for(int j=0; j<3; j++)
      {
          cout << MatrixA[i][j]; 
       }
    }

The input portion read in 9 x 3 values, for a total of 27. The output only displays a 3 x 3 matrix.

Which is it?

0

Since you are reading in 3x3 matrices, your loops should do just that. You have one input statement reading three values, so loop that just three times, not nine.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.