[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-10-25
7
Medium Priority
?
212 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 total points
ID: 9620766
Did you remember to remove the infile >> m in the while loop?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 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