Jump to content

multiline string, endl, \n, line space

- - - - -

  • Please log in to reply
9 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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


#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, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Difference between \n and std::endl is that endl flushes buffer as well as prints new line, more info here. Note that \n is faster than endl.

\n is an escape sequence for new line, it will always print new line, unless you escape it (put another \ in front of it), then it'll print \n without going into new line.

std::cout << "new \n line"; // new

                            // line

std::cout << "new \\n line"; // new \n line


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The main difference will be how many times you call operator<<
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Dutchman, WingedPanther.

Dutchman, if you don't mind, could you please give me some information about the painting used in your avatar? It would be nice of you.

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

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

jackson6612 said:

Dutchman, if you don't mind, could you please give me some information about the painting used in your avatar? It would be nice of you.

It appears to be G-Man, from the introduction of the Half Life 2 series of games.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Alexander said:

It appears to be G-Man, from the introduction of the Half Life 2 series of games.

Thanks, Alexander.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#7
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Hmm, just realized he was referring to Flying Dutchman's and not Alexanders avatar... next time I will read carefully. -_-

Also, when you put two constant strings next to each other:
    cout << "My fist string" "Oh no another one right next to it!";
The compiler will simply automatically concatenate them together. This is not the case with any other char array, just so you know.
Wow I changed my sig!

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

ZekeDragon said:

Hmm, just realized he was referring to Flying Dutchman's and not Alexanders avatar... next time I will read carefully. -_-

Also, when you put two constant strings next to each other:
    cout << "My fist string" "Oh no another one right next to it!";
The compiler will simply automatically concatenate them together. This is not the case with any other char array, just so you know.

Hi ZekeDragon

I couldn't understand the meaning of "just so" in the above context. As I'm sure you have already noticed I'm not native English speaker.

The dictionary says:
just so: in a careful manner; "you must treat this plant just so".

Seb said:

Language doesn't have speed. If an implementation were to optimise std::endl to be exactly the same as an appended '\n', how would one be faster than the other? ;)

Moral of the story: Don't confuse implementation with specification.

Hi Seb

So now should I request you to tell me the difference between the implementation and specification in the C++ context?!:)

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

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

jackson6612 said:

I couldn't understand the meaning of "just so" in the above context.
What ZekeDragon was pointing out, is that the compiler often lets you perform special operations with constant character arrays. If your character array cannot be modified, then it is constant.

One example of this is a string wrapped in double quotes when passed to a function or statement: "this is a string". This string cannot be modified unless it is in a variable, so the compiler may or may not let you combine the strings together. An example in C and C++:

In C:
printf("%s", "combining" "some" "constant" "strings"); 
and C++ as pointed out
cout << "combining" "some" "constant" "strings"
As you may notice, they are not variables but are constant, so the compiler happens to let them be combined for the ease of the programmer.

jackson6612 said:

So now should I request you to tell me the difference between the implementation and specification in the C++ context?!:)

We can use our previous example to answer this. The specification may not explicitly allow two constant strings to be combined automatically - But the implementation of the specification may allow this.

The implementation can do anything, and not follow the specification of the C/C++ languages exactly. This can be because the compiler will want to help the programmer (as in our previous example), or it may not support a certain data type, and need to create special code to compensate for that.

That special code may create new data types or functions to support them that are not part of the specification.

I hope I was clear on my explanation.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Actually the C standard requires consecutive string constants to be concatenated. And the C++ standard is for the most part a super set of C.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users