Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. tricky question about endl

tricky question about endl

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erich Ruth
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • E Erich 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

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      cout.flush() will do it.

      E 1 Reply Last reply
      0
      • M Michael Dunn

        cout.flush() will do it.

        E Offline
        E Offline
        Erich Ruth
        wrote on last edited by
        #3

        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)

        M 1 Reply Last reply
        0
        • E Erich Ruth

          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)

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          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. Flushing cout means that any text waiting to be printed is immediately printed. Since the endl manipulator does an "end of line", it flushes cout. Calling cout.flush() does the same thing, just without going to a new line. If the presence/absence of a cout 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 have cout << endl;? --Mike-- http://home.inreach.com/mdunn/

          E 1 Reply Last reply
          0
          • M Michael Dunn

            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. Flushing cout means that any text waiting to be printed is immediately printed. Since the endl manipulator does an "end of line", it flushes cout. Calling cout.flush() does the same thing, just without going to a new line. If the presence/absence of a cout 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 have cout << endl;? --Mike-- http://home.inreach.com/mdunn/

            E Offline
            E Offline
            Erich Ruth
            wrote on last edited by
            #5

            > 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

            E 1 Reply Last reply
            0
            • E Erich 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

              E Offline
              E Offline
              Erik Funkenbusch
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups