I need to have the same following format of dadat, which are read in a text fil, in output.
"Mainnumber" is the first number of each matrix (1,2,3) and for each "mainnumber" (i), the number of rows are determined for each (i) by ("nof_row_per_mainnumber[i]"= numberof_rows;).
For every "Mainnumber" (matrix) it starts from the first row ("first_rows[i]"= k;).
The problem is that it says "numberof_rows" should be initialized.
1
2 3

2
4 5
6 7

3
8 9
3 5
2 6

class Classname {  
            int mainnumber;
            int nof_row_per_mainnumber[3];
            int numberof_rows;
        int first_rows[3];
            double first_array[6];
            double second_array[6];
            int total_rows;
    public:
        void Read(char *fname);
        void Writedata();
    }
    void Classname::Read(char *fname)
    {   int i, j;
        ifstream fin(fname);
            fin >> 3;
            int numberof_rows;
        int k= 0;
        for(i= 0; i<mainnumber; i++) {
            fin >> numberof_rows;
            no_row_per_mainnumber[i]= numberof_rows;
            first_rows[i]= k;
            for(j= 0; j<numberof_rows; j++) {    // read each row
                fin >> first_array[k];
                fin >> second_array[k];
                k++;
            }
        }
        total_rows= k;
    }
    void Classname::Writedata()
    {   // writes model to cout in same format it was read in
            int numberof_rows;
        int k= 0;
        for(i= 0; i<mainnumber; i++) {
            cout << i+1 << endl;
            no_row_per_mainnumber[i]= numberof_rows;
            first_rows[i]= k;
            for(j= 0; j<numberof_rows; j++) {    // write each line
                cout << first_array[k] << " " << second_array[k] << " ";
                cout << endl;  
                k++;
            }
        }
        total_rows= k;
    }

Firstly, your code as is shouldn't compile.

  • fin >> 3;- makes no sense. You can't can't extract an input stream to a constant value.

  • int nof_row_per_mainnumber[3];- this isn't spelled the same as where you use it, no_row_per_mainnumber[i]= numberof_rows;

  • The initial class block must end in a ;.

  • ifstream fin(fname);- this needs a header reference(#include <fstream>) and std:: in front std::ifstream fin(fname);

  • for(i= 0; i<mainnumber; i++)- i isn't declared properly

  • cout << i+1 << endl;- this needs a header reference(#include <iostream>), and std:: in front

There's probably more but this should get you started.

Thanks, I edited the previous one, there are headers in the project file, but i didnt write them.

class Classname {  

        int mainnumber;
        int nof_row_per_mainnumber[3];
        int numberof_rows;
    int first_rows[3];
        double first_array[6];
    double second_array[6];
        int total_rows;

public:
    void Read(char *fname);
    void Writedata();
}
void Classname::Read(char *fname)
{   int i, j;
    ifstream fin(fname);
        fin >> numberof_rows;
        int numberof_rows;
    int k= 0;
    for(i= 0; i<mainnumber; i++) {
        fin >> numberof_rows;
        nof_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // read each row
            fin >> first_array[k];
            fin >> second_array[k];
            k++;
        }
    }
    total_rows= k;
}
void Classname::Writedata()
{   // writes model to cout in same format it was read in

    int i, j;
    int numberof_rows;
    int k= 0;

    for(i= 0; i<mainnumber; i++) {
        cout << i+1 << endl;
        nof_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // write each line
            cout << first_array[k] << " " << second_array[k] << " ";
            cout << endl;  
            k++;
        }
    }
    total_rows= k;
}

In the Read loop it is "fin >> mainnumber;"

It appears to me you're making it more complicated than it needs to be. Since each row contains only 2 elements a simple struct to represent the row will simplify things quite a bit. Then the only members you need in the class is the number of rows, an array of rows and if necessary an accumulator to reperesent the total number of rows for all the matrices:

class Matrix 
{
    struct Row
    {
        int numa = -1;
        int numb = -1;
    };
    int numOfRows;
    Row rows[3];
    static int totalRows;
public:
    Matrix();
    static void Read(char *fname, Matrix input[]);
    static void Writedata(Matrix input[], size_t size);
};

The functions for handling the data can now be static, or external. They are also simplified greatly:

int Matrix::totalRows = 0;
Matrix::Matrix()
{
    numOfRows = 0;
}
void Matrix::Read(char *fname, Matrix input[])
{
    int i = 0, m_count = 0;
    string temp = "";
    ifstream fin(fname);
    while (fin >> input[m_count].numOfRows)
    {
        for (; i < input[m_count].numOfRows; i++)
        {// read each row
            fin >> input[m_count].rows[i].numa >> input[m_count].rows[i].numb;
        }
        m_count++;
        getline(fin, temp);//Read empty line
        Matrix::totalRows += i;
        i = 0;
    }

}
void Matrix::Writedata(Matrix input[], size_t size)
{ // writes model to cout in same format it was read in
    size_t m_count = 0;
    while (m_count < size)
    {
        cout << input[m_count].numOfRows << '\n';
        for (size_t i = 0; i < input[m_count].numOfRows; i++)
        {
            cout << input[m_count].rows[i].numa << " " << input[m_count].rows[i].numb << " \n";
        }
        m_count++;
        cout << '\n';//Add empty line as per original file
    }
    cout << "Total number of rows = " << totalRows << '\n';
}

Using the functions would look something like this:

Matrix test[3];
Matrix::Read("text.txt", test);
Matrix::Writedata(test,3);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.