What I'm trying to achieve is have the matrices that I have in two separate text files printed into the console upon request of the user of the program. However, in the future operations need to be performed on the matrices so somebody told me that I need to have the program recognise the text files as a matrix. If that makes sense?
So I gave it a go and here is the code I have so far. So for Matrix A all I am getting is
1
1
1
where I would like
1 0 0
0 2 0
0 0 3
As for matrix B well this works in the fact that it prints the text file but doesn't read it as a matrix.
I just don't know how to go about getting what I need so if anyone can help that'd be great!
#include <iostream> //declaring variables
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string code(string& line);
int main()
{
int MatrixA[3][3] = {{1,0,0},{0,2,0},{0,0,3}};
ofstream outf;
ifstream myfile;
string infile;
string line;
string outfile;
cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
cin >> infile; //prompts user for input file
if (infile == "A.txt")
{ //read whats in it and write to screen
myfile.open("A.txt");
cout << endl;
while (getline (myfile, line))
cout << MatrixA[0][0] << endl;
}
else
if (infile == "B.txt")
{
myfile.open("B.txt");
cout << endl;
while (getline (myfile, line))
cout << line << endl;
}
else
{
cout << "Unable to open file." << endl;
}
//{
//while("Choose next operation");
//}
return 0;
}
I now have this
if (infile == "A.txt")
{ //read whats in it and write to screen
int j=0;
myfile.open("A.txt");
cout << endl;
while (line>>MatA[j][0]>>MatA[j][1]>>MatA[j][2]); j++;
//while (getline (myfile, line))
cout << line << endl;
//float elements[9];
//ifstream myfile("A.txt");
//myfile >> elements[0] >> elements[1] >> elements[2];
//myfile >> elements[3] >> elements[4] >> elements[5];
//myfile >> elements[6] >> elements[7] >> elements[8];
//myfile.close();
But where the 'line>>' is the >> has an error saying no operators match these operands? Could you explain what this means please? And suggestions on how it could be resolved?