[VIEWED 7699
TIMES]
|
SAVE! for ease of future access.
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-11-09 12:10
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Here is my piece of code which is bothering me really hard. Basically what this does is read string from of data file.This piece which some how does not read the end of file after all the date in the file are being read. it goes round again and fill the matrix (actually vector) with the last 'num' variable it had read in the loop it had gone through last time. cTable and dTable are filled with same num variable it had read in the previous loop cycle. for example, if 5 is the last integer 'num' read than it comes round and fill the vectores with 5s. Case 'E' is the last case in this switch statement and of course 'ch' still has 'E' in it. do { input >> ch; switch (ch) { case 'E': // To read the cofficient of linear equations and find the solutions. input >> bROW; if ( bROW > MAX && bROW <= 0) YesNo(); // if out of bound, asks if want to do it again with another test file. ClearSMat(bROW); // clears the vectors for next use for(int i = 0; i < bROW; i++) { for(int j = 0; j <= bROW; j++) { input >> num; if (j == bROW) dTable[i] = num; else cTable[i][j] = num; } } SystemOfEqMatrix(bROW);// print the system of linear equations in matrix form FindSolution(bROW); // find the solution of the linear equations SolutionMatrix(bROW); // print the solutions //cin.ignore(1, '/n'); break; }//end of switch } while (!input.eof());//end of do - while
|
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-11-09 12:13
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
does not read the end of file and goes infinitely.
|
|
|
techGuy
Please log in to subscribe to techGuy's postings.
Posted on 02-11-09 2:37
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
do { input >> ch; cout< <"ch is "<<ch; input>>bRow; cout<<"bRow is "<<bRow; input>>num; cout<<"num is "<<num; } while (!input.eof());//end of do - while
Can you see what's the output of this?
Last edited: 11-Feb-09 02:39 PM
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-11-09 3:02
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
TechGuy, It does what it is suppose to do... it gives me correct solution and correct matrix.. but somehow it does not read eof() and goes back to fill the vectors again with the last digit which it had already done with. I tested with couple of files to see if it is got to do with the files, again it does the same thing. It simply does not read the eof() of the files I am testing with. this is the test file I am using. I for the inverse and E for system of equation. After I and E is the rows of the matrix.And I have put cursor at the ennd of the file too. I 3 1 1 0 4 3 0 2 1 -1 I 2 2 -4 4 -7 I 3 1 1 0 1 2 1 2 3 2 E 3 1 2 1 8 2 1 -1 1 1 1 -2 -3 E 3 1 1 0 20 1 2 1 30 2 3 2 55 thanks!!
|
|
|
Maverick
Please log in to subscribe to Maverick's postings.
Posted on 02-11-09 3:05
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Does your code read all the matrices or it just reads the first one?
Last edited: 11-Feb-09 03:13 PM
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-11-09 3:13
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
maverick, there are two cases I and E.. I gave here just E case cause E is the last case and after it read data from the file for case ' E' it should reach to the end of file. Ok here is my whole code in main() void main() { int aROW, bROW, num ; char ch; string fileName; ifstream input; cout << "Enter the file name : "; cin >> fileName; // Attempt to open a disk file input.open(fileName.data()); if (!input.is_open()) { cout << "\nFile could not be opened!\n" << endl; exit(1); // abort execution } do { input >> ch; switch (ch) { case 'I': // find the inverse of the given matrix, if it is ivertible input >> aROW; if ( aROW > MAX && aROW <= 0) YesNo(); ClearIMat(aROW); for(int i = 0; i < aROW; i++) { for(int j = 0; j < aROW; j++) { input >> num; aTable[i][j] = num; } } OriginalToInverse(aROW); //identity matrix for(int i = 0; i < aROW; i++) { for(int j = 0; j < aROW; j++) { if ( i == j) bTable[i][j] = 1; else bTable[i][j] = 0; } } FindInverse(aROW); InverseMatrix(aROW); break; case 'E': // find the solution of the linear equations input >> bROW; if ( bROW > MAX && bROW <= 0) YesNo(); ClearSMat(bROW); for(int i = 0; i < bROW; i++) { for(int j = 0; j <= bROW; j++) { input >> num; //if (num == 0) // break; if (j == bROW) dTable[i] = num; else cTable[i][j] = num; } } SystemOfEqMatrix(bROW);// print the system of linear equations in matrix form FindSolution(bROW); // find the solution of the linear equations SolutionMatrix(bROW); // print the solutions //cin.ignore(1, '/n'); break; }//end of switch } while (!input.eof());//end of do - while input.close(); } // end of main
|
|
|
Maverick
Please log in to subscribe to Maverick's postings.
Posted on 02-11-09 3:15
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Does your code read all the matrices or just the first one?
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-11-09 3:16
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
BTW, cTable is for storing the coefficient and dTable for storing constants after = in the equations.
|
|
|
techGuy
Please log in to subscribe to techGuy's postings.
Posted on 02-11-09 3:17
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
try using while(input>>ch) { } see if that works, i dont have c++ compiler with me right now...so can't verify it.
|
|
|
suike
Please log in to subscribe to suike's postings.
Posted on 02-11-09 4:13
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hi, I had the exact same question four years back. eof returns true only after a read effort is made after end of file. This code: do { read; do other stuff} while ( !eof) with this file: 1 2 Your code reads value 2, finishes do, checks to see if eof is true....which it is not right now, because you su ccessfully read 2. The code goes back to do...tries to read something....doesn't have anything....the variable still holds the value of 2....it is ONLY THIS TIME eof is true....the while loop terminates. There are two ways of getting around this: 1> as techguy suggested in the post above this: using while(inputstream>>stuff) 2> avoid do-while, use just while, and do read; while(!inputstream.eof()){blah; read} Gluck!
|
|
|
kaloVale
Please log in to subscribe to kaloVale's postings.
Posted on 02-12-09 12:05
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Thanks guys. I appreciate all of yours effort. finally I have found the mistake and corrected it. It was because of the test file I was using. Apparently I had one too may 'enters' after the last digit in the test file which caused the char Var 'ch' to look for eof without any avail because I pushed down eof with my enters. Each time 'ch' could not find eof it filled my matrices with the last digit it had read in the last loop. and BTW both do-while and while loops work perfectly with the new test file.
|
|