End of File-controlled while loop

This topic is 3633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 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 numberswhile( 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 on other sites
stringstream ss("1 2 3 4 "); // notice the space at the endwhile(!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;

Share on other sites
A common pattern in C++ is to make the stream-good check right as you read the variable:

std::istringstream file( "1 2 3 4" );for ( int i ; file >> i ; ) {    std::cout << i;}

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.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• Forum Statistics

• Total Topics
632593
• Total Posts
3007275

×