tricky question about endl
-
I have a function that keeps returning a value. For some reason, when I have at the end of that function cout << "." << endl; everything works, I get a new value each time. If I leave this command off or type cout << "."; I get an error in my calculations. Several sources say "the endl command outputs a newline character and flush the stream". How can I "flush the stream" without creating a new line? Any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth
-
I have a function that keeps returning a value. For some reason, when I have at the end of that function cout << "." << endl; everything works, I get a new value each time. If I leave this command off or type cout << "."; I get an error in my calculations. Several sources say "the endl command outputs a newline character and flush the stream". How can I "flush the stream" without creating a new line? Any response any one can give me will be greatly appreciated. Sincerely, Erich J. Ruth
cout.flush()
will do it. -
cout.flush()
will do it.I really appreciate your response. I have gotten responses from you before and they have always been helpful. For some reason, I still can't get my code to work. I managed to reduce it to the following error. If I type at the end of my function cout << "." << endl; the code works fine, but I get along list of periods before I get the answer. If I type cout << "."; or cout << "." << "\n"; or puts(""); or anything that doesn't have the endl command, then my calculations are way, way, way off. It does not make any sense to me why is works with endl. In a book I read, it says endl "flushes the stream", but the command cout.flush(): doesn't "flush" anything. I am going nuts! Please any response you can give me will be gretly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
-
I really appreciate your response. I have gotten responses from you before and they have always been helpful. For some reason, I still can't get my code to work. I managed to reduce it to the following error. If I type at the end of my function cout << "." << endl; the code works fine, but I get along list of periods before I get the answer. If I type cout << "."; or cout << "." << "\n"; or puts(""); or anything that doesn't have the endl command, then my calculations are way, way, way off. It does not make any sense to me why is works with endl. In a book I read, it says endl "flushes the stream", but the command cout.flush(): doesn't "flush" anything. I am going nuts! Please any response you can give me will be gretly appreciated. Sincerely, Erich J. Ruth (an overworked graduate student)
Let me go back one step and ask, do you know what flushing means? Standard output is normally line-buffered, so that any text sent to
cout
is not printed until an end-of-line character ('\n') is printed, or the program exits. Flushingcout
means that any text waiting to be printed is immediately printed. Since theendl
manipulator does an "end of line", it flushescout
. Callingcout.flush()
does the same thing, just without going to a new line. If the presence/absence of acout
method call is affecting code earlier in the function, I'd guess that you have a stack or buffer overrun somewhere in your code. How exactly is your code going wrong when you don't havecout << endl;
? --Mike-- http://home.inreach.com/mdunn/ -
Let me go back one step and ask, do you know what flushing means? Standard output is normally line-buffered, so that any text sent to
cout
is not printed until an end-of-line character ('\n') is printed, or the program exits. Flushingcout
means that any text waiting to be printed is immediately printed. Since theendl
manipulator does an "end of line", it flushescout
. Callingcout.flush()
does the same thing, just without going to a new line. If the presence/absence of acout
method call is affecting code earlier in the function, I'd guess that you have a stack or buffer overrun somewhere in your code. How exactly is your code going wrong when you don't havecout << endl;
? --Mike-- http://home.inreach.com/mdunn/> How exactly is your code going wrong when you don't have > cout << endl;? If I leave the endl, I get no real errors, the computer doesn't crash or anything, I just get the wrong calculations, almost like it forgets or confuses simple addition. If I insert endl, I get no errors and my calculations are dead-on. I have a vague clue as to what flushing is. I am stuck badly. Any other response, suggestions, anything will be greatly appreciated. Sincerely, Erich J. Ruth
-
> How exactly is your code going wrong when you don't have > cout << endl;? If I leave the endl, I get no real errors, the computer doesn't crash or anything, I just get the wrong calculations, almost like it forgets or confuses simple addition. If I insert endl, I get no errors and my calculations are dead-on. I have a vague clue as to what flushing is. I am stuck badly. Any other response, suggestions, anything will be greatly appreciated. Sincerely, Erich J. Ruth
You might try showing us how you are doing your calculations with some code. Showing us only the line withou cout doesn't help us figure out how you calculations can be mysteriously "wrong". Try to isolate it to as small of a section of code as you possibly can.