Quick for loop question

Status
Not open for further replies.

DecoyMG

Seniorius Lurkius
6
I am writing a program to simulate the Hardy-Weinberg principle (a biology theory about how allelic frequencies should stay the same within a population given...), but my question isn't about the principle. It is about a for loop I use in the program to simulate the selection of finding a mate and then the mating process (don't get any ideas, its a command line program, no graphics). I have it set to run the number of times the user specifies, but I was hoping to be able to give the user some sort of feedback as to how many mating processes have occured. To such ends I wrote this for loop:

//numbertimes is a value the user entered
for(int k=0; k<numbertimes; k++)
{
selectmate(creat, matematrix); //finds the mates
mateagainst(creat, matematrix, choice); //mates the organisms
if(k%5==0) //heres the problem
{
cout << " " << k << " ";
}
}

When I compile and run the program, I expect a nice list to slowly appear of the number of mating processes that have been completed. Instead the computer holds off and sends the entire list of numbers indicating the number of matings completed at once. I have tried it on several different computers and I get the same thing each time. Now I'm not sure if this is some problem with my compiler (I use DJGPP), but I was hoping some of you may have some insight into why I get the entire list at once. If there is anything else you need to know, go ahead and ask. Thanks

Decoy
 

dockeen

Ars Tribunus Militum
1,600
I can answer some of your question. C and C++ have a habit of buffering up print statements, such that they are printed at a convenient time. In C, there is a way to force it to do your printf when you command it, it involves flushing the buffer. This is an issue in general, not just ot your compiler. Let me see if I can hunt up how to force cout to occur when you want.

Wayne
 

dockeen

Ars Tribunus Militum
1,600
I believe I remember that you can do something like this:

cout << " "
<< k
<< " "
<< flush;

Forgive the fact that I format my cout stuff a little different from you, the key is the flush. I remember that this works with file streams, I think it will work in your case. I am sure some of the much smarter people here will correct me if I blew that one.

Wayne
 
<tt>cout</tt> is typically buffered by quite a lot (maybe a few hundred bytes or more); if you need stuff written to <tt>cout</tt> to display instantly, you need to flush it. You can, if you really want, shrink <tt>cout</tt>'s buffer to a single character, if you want to not have to flush it all the time, but it slows down considerably if you do.

The error streams (<tt>cerr</tt> and <tt>clog</tt>) are unit buffered by default, to avoid this kind of issue (for errors in particular, you don't want the information to be lost if your program crashes without flushing its buffers).
 
i didn't know that you could force a flush by appending "<<flush".
is there an advantage of one over the other?
It's just a matter of style. flush would be part of the iostream manipulators. Manipulators (as far as I know) simply call a member function of the stream class, which means all manipulators have equivalent member functions. Some are shortcuts though (ie. hex instead of setbase(16)).
 

bugbum1

Ars Scholae Palatinae
893
since you read cin, i'm not sure flush makes sense (you could just eat the stream). however, you should check input streams for EOF or stream failure:

if (cin.fail()) ...

or

if (!cin) ...

in general, i understand why "unit buffered" is used on error streams, but the definition i read said "unit buffered" is bit buffered, when it seems character buffered makes more sense for console type devices. did i read a bad definition for unit buffered, or is there a piece i'm lacking?

... edit looks like i got a bad definition. more reading says unit buffered is analogous to

cout.rdbuf()->setbuf(0,0);
 
Status
Not open for further replies.