multiline string, endl, \n, blank line space

This is a discussion on multiline string, endl, \n, blank line space within the C++ Programming forums, part of the General Programming Boards category; Hi I have been told that quotation marks are used to enclose string constants. In the code given below I ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    multiline string, endl, \n, blank line space

    Hi

    I have been told that quotation marks are used to enclose string constants. In the code given below I used different methods to insert a blank line space between the text lines.

    In the case of using '\n' you either have a choice to use it within the string, such as "Hello, how are you?\n"; , or to use it separately, such as "Hello, how are you?" << "\n";.

    In both cases I don't get how the compiler or C++ knows that I'm asking it to go to next line because in the first I'm using \n with in a string constant, and in the second it is simply with in quotation marks, and anything within the quotes is tread as text.

    This reminds me of another question. They say the distinction between endl and \n has something to do with the input buffer. What is it in simple words? Is there also a output butter?

    Thanks for all your help, and especially your time.

    Best regards
    Jackson

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
           
           cout << "Yes, I this is a crazy act. Utter madness. I suggest you start\ 
    thinking rationally" << endl;
    
          cout << endl;
    
          cout << "Yes, I this is a crazy act. Utter madness. I suggest you start"
          << " thinking rationally" << endl;
          
          cout << "\n";
          
          cout << "Yes, I this is a crazy act. Utter madness. I suggest you start "
          "thinking rationally" << endl;
          
          cout << " " << endl;
           
          system("pause");
           
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,508
    how the compiler or C++ know
    It doesn't. The device in which the string is displayed has to know it. It is a convention..from the UNIX times(not sure of that though).. that..when a console/printer...whatever...encounters a newline(and/or linefeed !!!) character, it goes to a new line.

    The second question....I'm not sure....I've also read it...but it'd be nice if someone would post a small snippet demonstrating the deviation of the results.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    in the first I'm using \n with in a string constant, and in the second it is simply with in quotation marks
    Don't you think that both are the same, that is 'string constant enclosed with quotation marks'?
    The only difference is that the former is sent in one pass, the latter is sent in two passes.

    std::endl inserts a newline character and flushes the buffer.
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,508
    ...could you show an example, where "flushing the buffer" makes a difference ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by manasij7479 View Post
    ...could you show an example, where "flushing the buffer" makes a difference ?
    Thanks you, manasij, kmdv.

    I would also like to see what that "flushing buffer" thing is.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Google does not bite.

    Flushing means transmitting data contained in a buffer to a file, network stream etc. For example, if your application had crashed before flushing data to the file, all the data in the buffer would have been lost (because it was not physically saved to the disk).
    I never put signature, but I decided to make an exception.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,508
    .....the meaning is not the problem...but I'm yet to find an example (for cout ...not with other stream objects)..where using std::endl is a good idea...
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi manasij

    Would you please tell me what your signature means: Every language has an optimization operator. In C++ that operator is ‘//’?

    Thanks
    Last edited by jackson6612; 04-20-2011 at 09:49 AM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,508
    Put meaningful comments in your code...that would save you around 50% time when you try reading it after a while...

    The word operator though a little exaggeration..fits nicely here...
    P.S..I did not make *this(my sig) quote...a great programmer(not sure who) did..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks, manasij. By the way, sorry for the misspelling of your username in the last post of mine. I have corrected it now.

    Regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  2. Check for blank spaces in a string
    By BENCHMARKMAN in forum C Programming
    Replies: 19
    Last Post: 03-12-2008, 07:14 PM
  3. Replies: 4
    Last Post: 12-01-2007, 04:10 PM
  4. Replies: 3
    Last Post: 04-27-2005, 12:50 PM
  5. testing for blank line
    By rippascal in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2002, 09:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21