I'm trying to read input from a file, and store this input in a dynamically allocated array in my matrix class.
Every time I run this code, the input and output are both correct, but it always exits with return value 3221226356
. Can anyone tell me what the problem is?
The input file is a .txt
file, and it contains the number of rows, number of elements, and it looks something like this:
10 15
000000000000000
000999000009900
000999000009900
000000900009900
000090000099900
000099900000000
090000900000000
099999999900000
000090090000000
000000000000000
10
is the number of rows, and 15
is the number of elements per row.
I have to read this matrix and put this into my matArray
. Every character is one Element
node.
#include <bits/stdc++.h>
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
class Element{
private:
char value;
int processed;
public:
Element(){
value = 0;
processed = 0;
}
void setContent(char cellValue){ value = cellValue; }
void setProcessed(int num1){ processed = num1; }
char getContent(){ return value; }
int getProcessed(){ return processed; }
};
class Matrix{
private:
Element* matArray;
int rows;
int cols;
public:
Matrix(int nRows, int nCols){
matArray = new Element[nRows * nCols];
rows = nRows;
cols = nCols;
}
void setInput(int r, int c, Element input){
matArray[r * rows + c] = input;
}
Element getInput(int r, int c){
return matArray[r * rows + c];
}
};
int main(){
int numRow = 0;
int numCol = 0;
string filename = "";
string whiteSpace;
string string1;
cin >> filename;
ifstream inFS;
filename = filename + ".txt";
inFS.open(filename.c_str());
if(inFS.fail()){
cout << "fail";
}
if(inFS.good()){
cout << "good" << endl;
}
inFS >> numRow;
inFS >> numCol;
Matrix mat1(numRow, numCol);
Element temp;
char display;
getline(inFS, whiteSpace);
//Reading the matrix from the file and inserting this information to mat1
for(int i = 0; i < numRow; i++){
getline(inFS, string1);
cout << endl;
for(int j = 0; j < numCol; j++){
temp = mat1.getInput(i, j);
temp.setContent(string1[j]);
mat1.setInput(i, j, temp);
cout << mat1.getInput(i, j).getContent() << " ";
}
}
cout << endl;
return 0;
}