Solved

Need an explaination of using a " do, while"  loop for end of file

Posted on 2003-10-25
7
201 Views
Last Modified: 2010-04-02
I'm using a while loop to take in information from a .dat until it reaches the end of the file, everything is working right
except that it adds an extra input from the file,
(there is a list of say 10 pairs of numbers, reads and computes them, displays the computions, but displays 11 computions, this should only be 10)

here is the way I have my loop set up
do
{

}
while (infile);

any suggestions for revising?

thanks for the help
0
Comment
Question by:Orlando15767
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 9620644
If doesn't reach the end of the file until you try to read the last time, so:
do
{
  infile >> myvar;
  process(myvar);
} while(infile);
Will fail the last time with infile >> myvar, but you don't check it until after process is called the last time.  You might want to change it to something like:
while(infile >> myvar)
{
  process(myvar);
}

Good luck.
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9620687
tried that and it is still giveing me and extra computation.

thanks for the help
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9620703
That works for me.  Could you post more code so that we can help you better?  Also, what is the format of the file?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Orlando15767
ID: 9620724
here is the whole loop, the files are .dat

while (infile)
{
            //outfile << "Enter first integer ";
            infile >> m;
            outfile << "\nThe first integer is: " << m << endl;
            //outfile << "Enter second integer ";
            infile >> n;
            outfile << "The second integer is: " << n << endl;
            
      if (m < n)
      {
            ntemp = n;
            mtemp = m;
            m = ntemp;
            n = mtemp;
      }
      
            r = n % m;
            while(r != 0)
      {
            n = m;
            m = r;
            r = n % m;
      }
      

            outfile << "\nThe GCD is: " << m << endl;
            outfile << "\n";
            outfile << "\n";
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9620729
You're running into the same problem that I described earlier.  infile when cast to a bool (technically cast to a void *) only fails when it reaches the end of file condition, and you're not reaching end of file until you do infile >> m;  Change it to:
while(infile >> m)
{
         outfile << "\nThe first integer is: " << m << endl;
          //outfile << "Enter second integer ";
          infile >> n;
          outfile << "The second integer is: " << n << endl;
         
     if (m < n)
     {
          ntemp = n;
          mtemp = m;
          m = ntemp;
          n = mtemp;
     }
     
          r = n % m;
          while(r != 0)
     {
          n = m;
          m = r;
          r = n % m;
     }
     

          outfile << "\nThe GCD is: " << m << endl;
          outfile << "\n";
          outfile << "\n";
}

while(infile >> m) will fail if it reaches the end of file while reading m.  Of course, there's still the possibility that you reach end of file while reading m (if the number of int's in the file is odd), but I'm guessing that won't be a problem.  Good luck.
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 9620758
when I change it to
while (infile >> m)
it skips over the pairs of integers

ex
list of int's ( 9 12 45 62 35 100)
the ones that are read into n and m are 12,45 and 35,100

thanks for the help
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 100 total points
ID: 9620766
Did you remember to remove the infile >> m in the while loop?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now