0

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?

2 Answers 2

1

Replace cout << MatrixA[0][0] << endl; to cout << line << endl; in the code above. Not sure that I understand what you mean by "recognize as a matrix". A text file is a text file and nothing more. If you have incorrect data in it, there is no way of determining whether it is a matrix, unless you do some error checking (like making sure each line contains the same number of elements).

Sign up to request clarification or add additional context in comments.

2 Comments

What I mean is that when I perform operations on the matrix, will the program recognise what's been printed as a matrix, if not how do I go about doing it? Hope that made it a little clearer?
In your program you just display the contents of A.txt to the screen. If you want to perform operations with the content, then you have to load it into a matrix. Define int mat[3][3]; then in the while loop do line >> mat[j][0] >> mat[j][1] >> mat[j][2]; j++;, where you initialize int j=0; before the while loop. This should read each line of the file, then put the contents into the matrix mat. You can then use mat to perform whatever operations you want.
1

This is a complete code that loads a 3 x 3 matrix from a file and displays it. Hope it helps.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    ifstream myfile("A.txt");
    string line;
    int MatA[3][3];
    int i=0;
    while (getline (myfile, line))
    {
        std::stringstream ss(line);
        for(int j=0; j<3; j++)
            ss >> MatA[i][j]; // load the i-th line in the j-th row of mat
        i++;
    }
    // display the loaded matrix
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
            cout<<MatA[i][j]<<" ";
        cout<<endl;
    }
}

1 Comment

Brilliant mate, thanks! I know you shouldn't comment saying thanks apparently but this needed it haha!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.