Solved

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

Posted on 2003-10-25
7
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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