Hi guys.
I need to write a program which reads from a text file the elements of 2 given matrices.
Excluding the text file business, i have written up a code that will just display the elements on the screen.
I have attempted to put it in a text file and get it to read it, which is what i'm assuming needs to happen but nothing worked.
Here's my code that just displays the elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
cout << "Matrix A" << endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "A[" << i+1 << "][" << j+1 << "]: ";
cout << A[i][j]<< endl;
}
cout<< " " << endl;
cout <<"Matrix B" <<endl;
for ( int i = 0; i < 3; i++ )
for ( int j = 0; j < 3; j++ )
{
cout << "B[" << i+1 << "][" << j+1 << "]: ";
cout << B[i][j]<< endl;
}
How do i bring in text files into this properly?
Please help, I'm new to this!
ok so ive made this sample and it seemed to work for me.
this is to read the file, assuming the file has all the needed numbers and have spaces between them, that code you posted is incorrect.
try to figure out from this if you can on how to write to a file, show me what you come up with. i can help you tonight when i comeback from work(i dont have enough time to write the rest yet), if noone else does it by then.
// use fstream to see if the exists
fstream file("sampledata.txt");
if (file)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
file >> A[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
file >> B[i][j];
}
}
}
else {
cout << "file not found";
}