Trying to input matrix from text file into Array

Say I have a text file named "matrix.txt" and it looks like this...

3
5 7 10
5 3 2
0 6 8

The first number on the line is the number of rows and columns there will be (there will always be the same number of rows and columns). After that there is the matrix. I've tried to make it work, but I keep getting the error "matrixA was not declared in this scope". I feel like there must be an easier way to do this. Any ideas?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main()
{
    ifstream file("matrix.txt");

    int num;
    int matrixSize = 0;
    int firstTime = 0;

    while(file >> num)
    {
       if(firstTime == 0)
       {
           matrixSize = num;
           firstTime++;
       }
    }

    matrixA = new int*[matrixSize];

    for (int i = 0; i < matrixSize; i++)
    {
        matrixA[i] = new int[matrixSize];

        for (int j = 0; j < matrixSize; j++)
        {
            file >> matrixA[i][j];
        }
    }

    file.close();
}
When you define a variable, you must give it a type. You never specify the type of matrixA.

int** matrixA = new int*[matrixSize];

It would probably be better to encapsulate the matrix behavior in a class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <iomanip>
#include <memory>
#include <fstream>
#include <sstream>


struct SquareMatrix
{
    SquareMatrix(unsigned dimensions) 
        : _dim(dimensions), _matrix(new int[_dim*_dim]) {}

    unsigned dimension() const { return _dim; }

    int& operator()(unsigned row, unsigned col)
    {
        return _matrix[row * _dim + col];
    }

    int operator()(unsigned row, unsigned col) const
    {
        return _matrix[row * _dim + col];
    }

private:
    unsigned _dim;
    std::unique_ptr<int[]> _matrix;
};

std::ostream& operator<<(std::ostream& os, const SquareMatrix& mat)
{
    for (unsigned i = 0; i < mat.dimension(); ++i)
    {
        for (unsigned j = 0; j < mat.dimension(); ++j)
            os << mat(i, j) << ' ';

        os << '\n';
    }
    return os;
}

std::istream& operator>>(std::istream& is, SquareMatrix& mat)
{
    for (unsigned i = 0; i < mat.dimension(); ++i)
        for (unsigned j = 0; j < mat.dimension(); ++j)
            is >> mat(i, j);

    return is;
}

int main()
{
    // std::ifstream in("matrix.txt");
    std::istringstream in("3\n5 7 10\n5 3 2\n0 6 8");

    unsigned size;
    in >> size;

    SquareMatrix matrix(size);
    in >> matrix;

    std::cout << matrix << '\n';
}


http://ideone.com/YlGB4E

Thanks a lot for that. But most of that is way over my head. Is there a simpler way to do this?
Any other ideas?
If you want to use pointers for the array the above
int** matrixA=new int*[matrixSize];
is the way to go. You can however declare it as a static array once you have the size as:
int matrixA[matrixSize][matrixSize];
I just dont know how to input the matrix...

3
5 7 10
5 3 2
0 6 8

When I do the "file >> num" command it only inputs the last number of the line (for example 10 on line 2). How do I input each number seperatly and ignore the spaces?
Topic archived. No new replies allowed.