OK, here is my dilemma, I am trying to read an input file into 3 arrays. The file must be formatted as such (ie - with each value on a new row):
3 - n value for an n x n matrix 'a'
1 - a11 - Next n^2 terms are a[j]
2 - a12
3 - a13
4 - a21
5 - a22
6 - a23
7 - a31
8 - a32
9 - a33
1 - y1 - first entry in solution vector of linear problem --> y[j]
2 - y2
3 - y3

The goal of the code is to solve Ax = y for x. I have done this successfully, but I can not get my input to read from a file. So far I have tried doing it step by step, reading the first line as n, the next n^2 as a[j] and the last as y[j]. However, I can't seem to do it without errors, particularly because I am not sure how to convert the input char matrix tempA to a double matrix A for my calculations. Any help would be great!

``````.........
int n;
char nn;

nn=0;

for (i = 0; i < 1; i++)
{
inputFile >> nn;
}

n=atoi(&nn);

char *tempA;
tempA = 0;

for (i=1; i<=(n^2); i++)
{
inputFile >> tempA[i];
}

char *tempP;
tempP = 0;

for (i=n^2; i<=(n^2+n); i++)
{
inputFile >> tempP[i];
}
inputFile.close();``````

This what you should be doing:

``````int main(){
ifstream fileInput("LinearEquations.txt"):
if(!fileInput) return -1; //error
//get the n value
int numOfMatrices = 0;
int squareSize = 0;
fileInput >> numOfMatrices >> squareSize;
std::vector<Matrix> matrices(numOfMatrices,Matrix(squareSize,squareSize) ):

//read in values here in a while loop
}``````

## All 2 Replies

This what you should be doing:

``````int main(){
ifstream fileInput("LinearEquations.txt"):
if(!fileInput) return -1; //error
//get the n value
int numOfMatrices = 0;
int squareSize = 0;
fileInput >> numOfMatrices >> squareSize;
std::vector<Matrix> matrices(numOfMatrices,Matrix(squareSize,squareSize) ):

//read in values here in a while loop
}``````

Thanks! I have a couple of questions. First, 'Matrix' would just be 'A' correct? Also, 'numOfMatrices' is what exactly? In this case I have an integer, a 3x3 matrix, and a vector-3 that I need to read in. Finally, will I still need to try and convert from char to int type? Thanks again.

Be a part of the DaniWeb community

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