Basically, first line has 4 numbers (each representing a specific variable values - the first nubmer which is 3, represents the dimension of the matrix), then second line through 4th line is a matrix (3 by 3 matrix), then the last line is the right-hand-side of that matrix. Thus I should be able to solve this linear equations eventually.
The problem comes from reading this file. At the moment I've got it so that:
if (!infile) {
cout << "Cannot open file " << inname << " for reading." << endl;
exit(EXIT_FAILURE);
}
infile >> dim >> ns >> s >> d; //Reads the first line (4 variables)
for (i = 1; i <= dim; i++) {
for (j = 1; j <= dim; j++) {
if (!(infile >> a[i][j])) {
cout << "Couldn't read a[" << i << "][" << j << "]" << endl;
exit(EXIT_FAILURE);
}
aa[i][j] = a[i][j];
}
}
.
.
.
other stuff
.
.
.
This results in the exit always (end up with the "couldn't read a[1][1]" error message).
I think the problem is that it doesn't go to the next line after reading the first 4 variables, and i'm sure there's an easy way of fixing this problem but my head is blank at the moment.
Oh it is read again in "matrix.txt" file...
The thing is that dim value updates as it is looped.
Thus I need to read in new updated-dim value everytime.
It is read first in a another file called "contour.txt", then it uses that dim value to decalre "a".
Then this function I put up above comes in later (with new dim value).
[source]
indx = ivector(1, dim); /* holds pivot info from ludcmp for lubskp
aa = matrix(1, dim, 1, dim); /* a copy of the matrix to send to ludcmp
x = vector(1, dim); /* Copy rhs to x and send to lubksp */
[/source]
The code is really lengthy so thus I didn't bother putting up the whole thing.
I don't know about matrix... I can't find anywhere the dmatrix() function...
I'll post an example on how to do the same thing with new() instead of dmatrix():
Thanks, I'll see what I can do with this.
It's end of the day so I might try it out tomorrow morning.
Could you check back again around tomorrow noon to see if I have any question?
I'll let you know how it went.
The if(!infile) works fine. But if you want to be completely accurate you can use:
if(!infile.is_open()){}
The is_open() returns true if the file is open and false otherwise.
[EDIT]
The infile is a stream. When trying to open the file it will have some contents (different than 0) so it will act as a True in the if.
If it returns an error then it is gonna have the value of 0 which is also a value for the False value.