Solved

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

Posted on 2003-10-25
7
204 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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