Solved

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

Posted on 2003-10-25
7
203 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 109
Find Visual Studio Tools 2 103
How to convert MFC APP to Win32 APP. 19 80
What is sub-make ? 2 60
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…
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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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