Sign in to follow this  
-Dingo-

End of File-controlled while loop

Recommended Posts

I'll skip all the details and get right to my question regarding end of file-controlled while loops. Anyway, we're starting to learn about them in my C++ class I'm taking and I cannot grasp the concetp of them to save my life. I understand other types of loops except for this one, but the examples that my book gives are very very vague. As an example (an example of when we would use an EoF while loop that was mentioned in class), say I had a list of numbers (just random numbers) and wanted to get the factorial function for all numbers 0 through 10 (though some of the random numbers were less than 0 or greater than 10) and I needed to use an End of File controlled while loop...how would I do that? I've browsed a couple websites looking for a clearer example of an EoF while loop but still cannot understand. If you have any info. on it, please let me know. Thank you.

Share this post


Link to post
Share on other sites
An "EoF while loop" is just a loop that stops after all the data has been read from a file.
while( not at end of file )
{
variable = read one variable from file
do something with variable
}
This is conceptually the same thing as the following loop, except the following loop just deals with arrays, not files:
int list[] = { 5, 10, 15, 20 };
int list_size = 4;
int current_index = 0;

while( current_index != list_size )//not at end of list
{
//read one variable from list
int variable = list[current_index];
++current_index;
//do something with variable
}

Quote:
Original post by -Dingo-
As an example (an example of when we would use an EoF while loop that was mentioned in class), say I had a list of numbers (just random numbers) and wanted to get the factorial function for all numbers 0 through 10 (though some of the random numbers were less than 0 or greater than 10) and I needed to use an End of File controlled while loop...how would I do that?
Firstly, I'm going to assume that this list of number is actually contained in a file (otherwise EoF is completely irrelevant!).

Secondly, the "do something with variable" part of the loop is a separate problem. First problem is how to get the list of numbers (using a EoF-loop), the second problem is then discarding any outside 0 through 10 and getting the factorial functions.
open file containing numbers
while( not at end of file )
{
variable = read one number from file
//do something with variable
if( number >=0 && number <= 10 )
answer = find_factorial( number );
}


[Edited by - Hodgman on October 9, 2008 11:08:54 PM]

Share this post


Link to post
Share on other sites
stringstream ss("1 2 3 4 "); // notice the space at the end
while(!ss.eof()) {
int i = 0;
ss >> i;
cout << i << " ";
}
prints 1 2 3 4 0
Remove the 0 at the end by reading the last space
ss >> std::ws;
or checking if the stream failed
if( ss.fail() ) break;
after a read.

Share this post


Link to post
Share on other sites
Quote:
Original post by -Dingo-
and I needed to use an End of File controlled while loop...how would I do that? I've browsed a couple websites looking for a clearer example of an EoF while loop but still cannot understand. If you have any info. on it, please let me know. Thank you.


Did you see any examples in class whatsoever? Did you find any examples in your browsing at all? If so, what didn't you understand about them? Post the code that you don't understand, and ask something specific about it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this