[Show all top banners]

kaloVale
Replies to this thread:

More by kaloVale
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 help needed computer geeks about coding!!
[VIEWED 7699 TIMES]
SAVE! for ease of future access.
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  

 
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.

 
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

 
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!!

 
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

 
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

 
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?

 
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.

 
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.

 
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 successfully 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!




 
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.

 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 60 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
ढ्याउ गर्दा दसैँको खसी गनाउच
Tourist Visa - Seeking Suggestions and Guidance
I hope all the fake Nepali refugee get deported
To Sajha admin
From Trump “I will revoke TPS, and deport them back to their country.”
wanna be ruled by stupid or an Idiot ?
MAGA denaturalization proposal!!
Travel Document for TPS (approved)
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
Those who are in TPS, what’s your backup plan?
How to Retrieve a Copy of Domestic Violence Complaint???
MAGA and all how do you feel about Trumps cabinet pick?
All the Qatar ailines from Nepal canceled to USA
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters