reading a text file into a 2d array problem | Newbie | | Join Date: May 2007
Posts: 6
| |
Hello.
I am trying to read a text file that contains
1
2
3
it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix[i][j] = {a,b,c};
At some point during tweaking all the matrix elements became 0 and I have lost myself.
Look at my code please.
I realise this is not the best programming way of writing code but I'm a scientist and I'd rather do it the way I have. If its too annoying to read then please suggest something else.
My code below. - #include<sstream>
-
#include <iostream>
-
#include<fstream>
-
#include <string>
-
#include <stdlib.h>
-
-
using namespace std;
-
int i,j;
-
int a,b,c;
-
-
-
int main ()
-
{
-
// getting the data from a text file
-
string str;
-
ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
-
// above opens the file
-
if (myfile.is_open()) // if open do the following
-
{
-
while (! myfile.eof() ) // while loop to do the below until the end of the file
-
{
-
getline(myfile, str);
-
cout<<str<<endl;
-
// matrix defining
-
int matrix[3][1]={i,j};
-
for (i=0; i<3; i++)
-
for (j=0; j<1; j++)
-
-
-
myfile >> matrix[0][0] >> matrix[1][0] >> matrix[2][0] >> ws;
-
-
matrix[0][0] = a;
-
matrix[1][0] = b;
-
matrix[2][0] = c;
-
-
int matrix[i][j] = {a, b, c}; // error here C2057 & C2087, due to the a=b=c=0 at the moment
-
-
// assigned the 3 numbers to the matrix elements
-
cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
-
-
//cout << matrix[i][j];
-
-
// attempted string to int conversion
-
//p = atof(line );
-
-
-
}
-
-
myfile.close();
-
}
-
-
-
else cout << "Unable to open file";
-
-
return 0;
-
}
thanks
Ben
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: reading a text file into a 2d array problem
Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values.
What exactly are you trying to do? I imagine there's a much simpler, cleaner option than all this.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,379
| | | re: reading a text file into a 2d array problem
I think you are making this too hard.
First, there really aren't 2D arrays in C++. There are only one-dimensional arrays. That is: -
int matrix[6] = {1,2,3,4,5,6};
-
is laid out in memory as 1 2 3 4 5 6.
Whereas this array: -
int arr[3][2] = {1,2,3,4,5,6};
-
is laid out in memory as 1 2 3 4 5 6.
Kinda the same, right?
The "dimensions" are really descriptions of the elements. In the first example there are 6 elements in the matrix array and each element is an int. In the second example, there are 3 elements in the array and each element is an array of 2 int.
You use the "dimensions" as a convenience for doing math.
However, since all arrays are laid out serially in memory, I suggest you write your 2D array out as individual integers starting with &arr[0][0]. For example, an int arr[10][4] has 40 integers. Start with &arr[0][0] and write out 40 ints.
On the restore, start at &arr[0][0] and read 40 ints.
Once that's done you can use your array as a 2D array as you normally would.
| | Newbie | | Join Date: May 2007
Posts: 6
| | | re: reading a text file into a 2d array problem
Well, you never initialize a, b, and c in your program (which in itself is not a good idea), but then, after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively. Not only does this override whatever information you pulled from the file, but it fills the array with garbage values. Quote:
Originally Posted by Ganon11 What exactly are you trying to do? I imagine there's a much simpler, cleaner option than all this.
I'm trying to read values from a text file and make a "matrix". As c++ doesnt understand what a matrix is without a "matrix header file" i'm using arrays. I wanted to be able to call matrix[i][j] and the output should be 1,2,3 which was read from the text file.
This matrix will be used in matrix equation algebra. The matrix represents a 3d point and I'm trying to translate and rotate this 3d point to match up with another 3d point. I have all the maths equations its just trying to implement them in c++ that's the dificulty. I'm at home and can't tell you exactly what maths will be used on the matrix. Does that answer your question?
[quote=Ganon11] after you input into matrix[0][0], matrix[0][1], and matrix[0][2], you assign each of these three slots to a, b, and c, respectively
I was trying to read the text file values into each element of the matrix, when this didnt work I thought it was due a string to int problem. I thought that by assigning matrix[0][0] to an int a it would change the string to an integer. Basically I'm unsure if reading from the text file gives me a string or because the file only contains number if it will be an int. I see now what I did is problematic but I don't see how to do what I want.
also how do initialize a,b,c? I said they were int at the beginning of the code but you mean initialize a value? I have seen people do a=b=c=0 before using them but does that then not make them 0 and I wanted them to be values from the file.
| | Newbie | | Join Date: May 2007
Posts: 6
| | | re: reading a text file into a 2d array problem
weaknessforcats I have made it too dificult but I really need to stick to the matrix[i][j] format. This is so I understand what I'm doing in terms of matrices or else I may get confused. I could individually assign elements like matrix[0][0] which is what I was doing but I need to be able to get an output for matrix[i][j] which should read 1,2,3 So I can use it in calculations later.
Thanks for the talk on arrays but I don't think what you suggested will help, my fault for not specifying what I wanted to do or aim to get.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,379
| | | re: reading a text file into a 2d array problem
Yes, you have made it too difficult. However, at the risk of confusing you, I took your code and twiddled it to illustrate what I said: -
#include<sstream>
-
#include <iostream>
-
#include<fstream>
-
#include <string>
-
#include <stdlib.h>
-
-
using namespace std;
-
int main ()
-
{
-
//
-
//The target matrix
-
//
-
int matrix[3][1];
-
int* input = &matrix[0][0];
-
// getting the data from a text file
-
string str;
-
//ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
-
ifstream myfile("C:\\scratch\\instructor\\Data set 1.txt", ios::in);
-
// above opens the file
-
if (myfile.is_open()) // if open do the following
-
{
-
while (! myfile.eof() ) // while loop to do the below until the end of the file
-
{
-
myfile >> *input; //put the int in the matrix array
-
++input; //set up for the next element
-
-
}
-
-
-
}
-
else
-
{
-
cout << "Unable to open file";
-
}
-
-
// assigned the 3 numbers to the matrix elements
-
cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
-
-
return 0;
-
}
-
First, I declared your matrix at the beginning of main()
Second, I declared an int* to contain the address of matrix[0][0]
Third, I de-reference the the int* when I use >>
Fourth, I increment the int* after using it to set up for the next element
Fifth, At the end of main(), I display the 2D matrix and there are your integers.
| | Newbie | | Join Date: May 2007
Posts: 6
| | | re: reading a text file into a 2d array problem
First, I declared your matrix at the beginning of main()
Second, I declared an int* to contain the address of matrix[0][0]
Third, I de-reference the the int* when I use >>
Fourth, I increment the int* after using it to set up for the next element
Fifth, At the end of main(), I display the 2D matrix and there are your integers.[/quote]
I understand now what you meant and see how yours works and mine didn't. I do have one question though.
Why can I not output matrix[3][1] and it give me 1,2,3? Do I always have to work with individual elements of the array?
I will be doing some addition, multiplication etc and was hoping not to have to do it using individual elements. If thats the only way to do it then obviously I will, its just later I intend on taking a set of matrice's and it would get tedious doing say 10 matrices.
Thanks for the help you guys I shall press on and if I get stuck I know where to come.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,379
| | | re: reading a text file into a 2d array problem Quote:
Originally Posted by bodowpin Why can I not output matrix[3][1] and it give me 1,2,3? Do I always have to work with individual elements of the array? Your example is matrix[3][1]. That is 3 arrays with elements that are arrays of 1 int each.
So, matrix[0] is the element 0 in matrix. and matrix[0][1] is element 0 in
matrix[0].
This is identical to matrix[3]. matrix[3] is an array of 3 int.
You will work with individual elements when you work with the ints themselves.
| | Newbie | | Join Date: May 2007
Posts: 6
| | | re: reading a text file into a 2d array problem Quote:
Originally Posted by weaknessforcats Your example is matrix[3][1]. That is 3 arrays with elements that are arrays of 1 int each.
So, matrix[0] is the element 0 in matrix. and matrix[0][1] is element 0 in
matrix[0].
This is identical to matrix[3]. matrix[3] is an array of 3 int.
You will work with individual elements when you work with the ints themselves.
From what you mentioned, is it possible to have matrix[3][1] give 1,2,3 if i didn't assign 1,2 or 3 as int's.
Sorry if this is a dumb question.
| | Newbie | | Join Date: May 2007
Posts: 6
| | | re: reading a text file into a 2d array problem
Another question related to my code.
I have moved on and am now trying to implement an svd algirithm to my code.
My huge code is below,
#include<sstream>
#include <iostream>
#include<fstream>
#include <string>
#include <stdlib.h>
#include <math.h>
#include "nrutil.h"
using namespace std;
int main ()
{
//
// matrix a definition
//
int amatrix[3][1];
int* inputa = &amatrix[0][0];
// getting the data from a text file
string stra;
ifstream myfilea("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
// above opens the file
if (myfilea.is_open()) // if open do the following
{
while (! myfilea.eof() ) // while loop to do the below until the end of the file
{
myfilea >> *inputa; //put the int in the matrix array
++inputa; //set up for the next element
}
}
else
{
cout << "Unable to open file";
}
// assigned the 3 numbers to the matrix elements
//cout << amatrix[0][0] << " " << amatrix[1][0] << " " << amatrix[2][0] << endl;
//
//matrixb definition
//
int bmatrix[3][1];
int* inputb = &bmatrix[0][0];
// getting the data from a text file
string strb;
ifstream myfileb("C:\\brooks\\C++ examples\\Data set 2.txt", ios::in);
// above opens the file
if (myfileb.is_open()) // if open do the following
{
while (! myfileb.eof() ) // while loop to do the below until the end of the file
{
myfileb >> *inputb; //put the int in the matrix array
++inputb; //set up for the next element
}
}
else
{
cout << "Unable to open file";
}
// assigned the 3 numbers to the matrix elements, this is my b matrix
//cout << bmatrix[0][0] << " " << bmatrix[1][0] << " " << bmatrix[2][0] << endl;
//matrix a is pi.
// for this example pi = p due to only using one matrix per data set.
// matrix b is p'i.
// for this example p'i=p' due to only using one matrix per data set.
// from Haung and Blostein's paper, " least squares fitting of Two 3-d point sets
// qi = pi - p, q'i = p'i - p'.
// qi matrix definition
int qmatrix[3][1];
int* inputq = &qmatrix[0][0];
// q'i matrix definition
int qamatrix[3][1];
int* inputqa = &qamatrix[0][0];
// qi = matrixa - matrixb, q'i = matrixa - matrixb.
// have to do them as seperate elements
// qi matrix below
qmatrix[0][0] = amatrix[0][0] - bmatrix[0][0];
qmatrix[1][0] = amatrix[1][0] - bmatrix[1][0];
qmatrix[2][0] = amatrix[2][0] - bmatrix[2][0];
//cout<< qmatrix[0][0] << qmatrix[1][0] <<qmatrix[2][0];
//q'i matrix below
qamatrix[0][0] = amatrix[0][0] - bmatrix[0][0];
qamatrix[1][0] = amatrix[1][0] - bmatrix[1][0];
qamatrix[2][0] = amatrix[2][0] - bmatrix[2][0];
cout<< qamatrix[0][0]<<" "<< qamatrix[1][0]<<" "<<qamatrix[2][0]<< "\n";
// H is defined by equation 11, sum of (qi q'i).
// H is a 3 by 3 matrix.
int Hmatrix[3][3];
int* inputH = &Hmatrix[0][0];
Hmatrix[0][0]= qmatrix[0][0]*qamatrix[0][0];
Hmatrix[0][1]= qmatrix[0][0]*qamatrix[1][0];
Hmatrix[0][2]= qmatrix[0][0]*qamatrix[2][0];
Hmatrix[1][0]= qmatrix[1][0]*qamatrix[0][0];
Hmatrix[1][1]= qmatrix[1][0]*qamatrix[1][0];
Hmatrix[1][2]= qmatrix[1][0]*qamatrix[2][0];
Hmatrix[2][0]= qmatrix[2][0]*qamatrix[0][0];
Hmatrix[2][1]= qmatrix[2][0]*qamatrix[1][0];
Hmatrix[2][2]= qmatrix[2][0]*qamatrix[2][0];
cout<< Hmatrix[0][0]<< " "<< Hmatrix[0][1]<<" "<< Hmatrix[0][2]<<" "<< Hmatrix[1][0]<<" "<< Hmatrix[1][1]<<" "<< Hmatrix[1][2]<<" "<< Hmatrix[2][0]<<" "<< Hmatrix[2][1]<<" "<< Hmatrix[2][2];
////////////////////////////////////////// SVD of a NxM Matrix //////////////////////////////////
int n;
int m;
float **v;
float w[3]; // for our case n is 3.
float **a;
void svdcmp(**a, m,n, w[3], **v);
/*Given a matrix a[1..m][1..n], this routine computes its singular value decomposition, A =
U•W•V T. Thematrix U replaces a on output. The diagonal matrix of singular values W is output
as a vector w[1..n]. Thematrix V (not the transpose V T ) is output as v[1..n][1..n].*/
{
Hmatrix[3][3] = a[m][n]; // i added this to make Hmatrix be the same as below
float pythag(float a,float b);
int flag,i,its,j,jj,k,l,nm;
float anorm,c,f,g,h,s,scale,x,y,z,*rv1;
rv1=vector(1,n);
g=scale=anorm=0.0; //Householder reduction to bidiagonal form.
for (i=1;i<=n;i++) {
l=i+1;
rv1[i]=scale*g;
g=s=scale=0.0;
if (i <= m) {
for (k=i;k<=m;k++) scale += fabs(a[k][i]);
if (scale) {
for (k=i;k<=m;k++) {
a[k][i] /= scale;
s += a[k][i]*a[k][i];
}
f=a[i][i];
g = -SIGN(sqrt(s),f);
h=f*g-s;
a[i][i]=f-g;
for (j=l;j<=n;j++) {
for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
f=s/h;
for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
}
for (k=i;k<=m;k++) a[k][i] *= scale;
}
}
w[i]=scale *g;
g=s=scale=0.0;
if (i <= m && i != n) {
for (k=l;k<=n;k++) scale += fabs(a[i][k]);
if (scale) {
for (k=l;k<=n;k++) {
a[i][k] /= scale;
s += a[i][k]*a[i][k];
}
f=a[i][l];
g = -SIGN(sqrt(s),f);
h=f*g-s;
a[i][l]=f-g;
for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
for (j=l;j<=m;j++) {
for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
}
for (k=l;k<=n;k++) a[i][k] *= scale;
}
}
anorm=FMAX(anorm,(fabs(w[i])+fabs(rv1[i])));
}
for (i=n;i>=1;i--) { //Accumulation of right-hand transformations.
if (i < n) {
if (g) {
for (j=l;j<=n;j++) //Double division to avoid possible underflow.
v[j][i]=(a[i][j]/a[i][l])/g;
for (j=l;j<=n;j++) {
for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j];
for (k=l;k<=n;k++) v[k][j] += s*v[k][i];
}
}
for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0;
}
v[i][i]=1.0;
g=rv1[i];
l=i;
}
for (i=IMIN(m,n);i>=1;i--) { //Accumulation of left-hand transformations.
l=i+1;
g=w[i];
for (j=l;j<=n;j++) a[i][j]=0.0;
if (g) {
g=1.0/g;
for (j=l;j<=n;j++) {
for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
f=(s/a[i][i])*g;
for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
}
for (j=i;j<=m;j++) a[j][i] *= g;
} else for (j=i;j<=m;j++) a[j][i]=0.0;
++a[i][i];
}
for (k=n;k>=1;k--) { //Diagonalization of the bidiagonal form: Loop over singular values,
for (its=1;its<=30;its++) { //and over allowed iterations.
flag=1;
for (l=k;l>=1;l--) { //Test for splitting.
nm=l-1; //Note that rv1[1] is always zero.*/
if ((float)(fabs(rv1[l])+anorm) == anorm) {
flag=0;
break;
}
if ((float)(fabs(w[nm])+anorm) == anorm) break;
}
if (flag) {
c=0.0; //Cancellation of rv1[l], if l > 1.
s=1.0;
for (i=l;i<=k;i++) {
f=s*rv1[i];
rv1[i]=c*rv1[i];
if ((float)(fabs(f)+anorm) == anorm) break;
g=w[i];
h=pythag(f,g);
w[i]=h;
h=1.0/h;
c=g*h;
s = -f*h;
for (j=1;j<=m;j++) {
y=a[j][nm];
z=a[j][i];
a[j][nm]=y*c+z*s;
a[j][i]=z*c-y*s;
}
}
}
z=w[k];
if (l == k) { //Convergence.
if (z < 0.0) { //Singular value is made nonnegative.
w[k] = -z;
for (j=1;j<=n;j++) v[j][k] = -v[j][k];
}
break;
}
if (its == 30) nrerror("no convergence in 30 svdcmp iterations");
x=w[l]; //Shift from bottom 2-by-2 minor.
nm=k-1;
y=w[nm];
g=rv1[nm];
h=rv1[k];
f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
g=pythag(f,1.0);
f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
c=s=1.0; //Next QR transformation:
for (j=l;j<=nm;j++) {
i=j+1;
g=rv1[i];
y=w[i];
h=s*g;
g=c*g;
z=pythag(f,h);
rv1[j]=z;
c=f/z;
s=h/z;
f=x*c+g*s;
g = g*c-x*s;
h=y*s;
y *= c;
for (jj=1;jj<=n;jj++) {
x=v[jj][j];
z=v[jj][i];
v[jj][j]=x*c+z*s;
v[jj][i]=z*c-x*s;
}
z=pythag(f,h);
w[j]=z; //Rotation can be arbitrary if z = 0.
if (z) {
z=1.0/z;
c=f*z;
s=h*z;
}
f=c*g+s*y;
x=c*y-s*g;
for (jj=1;jj<=m;jj++) {
y=a[jj][j];
z=a[jj][i];
a[jj][j]=y*c+z*s;
a[jj][i]=z*c-y*s;
}
}
rv1[l]=0.0;
rv1[k]=f;
w[k]=x;
}
}
free_vector(rv1,1,n);
}
float pythag(float a, float b)
//Computes (a2 + b2)1/2 without destructive underflow or overflow.
{
float absa,absb;
absa=fabs(a);
absb=fabs(b);
if (absa > absb) return absa*sqrt(1.0+SQR(absb/absa));
else return (absb == 0.0 ? 0.0 : absb*sqrt(1.0+SQR(absa/absb)));
}
////////////////////////////////////////////////////////////////////////////////////////////////
return 0;
}
I get errors in the svd part of the code which I'm trying to iron out.
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : error C2182: 'svdcmp' : illegal use of type 'void'
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : error C2078: too many initializers
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(137) : warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(142) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(215) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(246) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(271) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(298) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(320) : error C2601: 'pythag' : local function definitions are illegal
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(13): this line contains a '{' which has not yet been matched
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(324) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
c:\brooks\visual studios 8 vc bin\myfirstprogram\task 1 rotation and translation\task 1 rotation and translation\task 1 rotation and translation.cpp(325) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
My issue is trying to make my Hmatrix be the a[n][m] matrix. One of the errors I was getting said that the Hmatrix was undefined, this occured when I put it in the svdcmp function but clearly I have defined it above.
So my question is how do I use my matrix in that svd algorithm. The algorithm was taken from numeric recipes in c, written by Press amognst others.
Thanks
| | Newbie | | Join Date: Jun 2007
Posts: 2
| | | re: reading a text file into a 2d array problem
Hi
This was a nice code.
I am doing somewhat similar code.
Can u help me for the following code.
I want to read a text file containing 2 d array .
Then I want to check the 1 st column and if condition is satisfied I want to use the values in that column and corresponding in 2nd column.
At the end I want to store again a 2D array in another text file.
Will you help me with some sample code.
You can directly mail me at dbhushan@gmail.com
Thanks!
Regards
Bhushan Quote:
Originally Posted by bodowpin Hello.
I am trying to read a text file that contains
1
2
3
it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix[i][j] = {a,b,c};
At some point during tweaking all the matrix elements became 0 and I have lost myself.
Look at my code please.
I realise this is not the best programming way of writing code but I'm a scientist and I'd rather do it the way I have. If its too annoying to read then please suggest something else.
My code below. - #include<sstream>
-
#include <iostream>
-
#include<fstream>
-
#include <string>
-
#include <stdlib.h>
-
-
using namespace std;
-
int i,j;
-
int a,b,c;
-
-
-
int main ()
-
{
-
// getting the data from a text file
-
string str;
-
ifstream myfile("C:\\brooks\\C++ examples\\Data set 1.txt", ios::in);
-
// above opens the file
-
if (myfile.is_open()) // if open do the following
-
{
-
while (! myfile.eof() ) // while loop to do the below until the end of the file
-
{
-
getline(myfile, str);
-
cout<<str<<endl;
-
// matrix defining
-
int matrix[3][1]={i,j};
-
for (i=0; i<3; i++)
-
for (j=0; j<1; j++)
-
-
-
myfile >> matrix[0][0] >> matrix[1][0] >> matrix[2][0] >> ws;
-
-
matrix[0][0] = a;
-
matrix[1][0] = b;
-
matrix[2][0] = c;
-
-
int matrix[i][j] = {a, b, c}; // error here C2057 & C2087, due to the a=b=c=0 at the moment
-
-
// assigned the 3 numbers to the matrix elements
-
cout << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << endl;
-
-
//cout << matrix[i][j];
-
-
// attempted string to int conversion
-
//p = atof(line );
-
-
-
}
-
-
myfile.close();
-
}
-
-
-
else cout << "Unable to open file";
-
-
return 0;
-
}
thanks
Ben |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|