I’m currently working on a problem that takes 2 matrix as its input.
I notice that when I use the same ifstream object to open 2 file, values of the second file get weird when I print it out.
This is not the case if I use the second ifstream object to take the input. I wanted to know what is happening and why can’t I use just one.
`
int main(){
ifstream fin;
int G[20][20];
int G2[20][20];
int n = 8;
fin.open("inMat1.txt");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
fin >> G[i][j];
fin.open("inMat2.txt");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
fin >> G2[i][j];
fin.close();
// print
cout << "Gn";
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
cout << G[i][j] << " ";
cout << endl;
}
cout << "G2n";
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
cout << G2[i][j] << " ";
cout << endl;
}
return 0;
}`
Here is the result, all 2 matrices should be positive integer numbers and less than 100
G
0 55 92 92 87 49 51 1
92 0 62 7 78 32 45 82
78 51 0 81 91 22 8 73
54 100 12 0 24 14 33 62
27 58 54 82 0 50 45 35
99 63 36 56 91 0 9 34
22 20 82 66 37 28 0 22
59 37 95 79 36 7 3 0
G2
-1 0 0 0 0 0 0 0
0 0 1239583408 30728 1459795768 0 1239352720 30728
0 0 0 0 791621423 791621423 791621423 791621423
0 0 0 0 0 0 0 0
1633824622 1392535667 1230197573 1298091599 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
1599031623 1314210124 1145391171 1397048415 1347376203 1279870559 1982807365 1815048801
Louis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.