0

I am writing a cout statement in C++, but the statement is very big so I pressed enter so that I can start from next line (not want to write full long statement in one line). It was working fine but if \n (new line) is the first character after hitting the enter as you can see the second line of code it was not working. So I just want to ask is there any way to start your code from the next line (continuing the previous line of code) after hitting enter.

cout<<"\nChoose the operation you want to perform :
\n1. To insert a node in the BST and in the tree \n2";
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • the answers [here](https://stackoverflow.com/questions/1135841/c-multiline-string-literal/1135862) show ways to define multi-line strings = – kmdreko Aug 18 '19 at 20:44
  • Possible duplicate of [C++ multiline string literal](https://stackoverflow.com/questions/1135841/c-multiline-string-literal) – L. F. Aug 19 '19 at 16:34

3 Answers3

1

Yes, you can this way:

std::cout << "\nChoose the operation you want to perform:\n"
             "1. To insert a node in the BST and in the tree\n"
             "2. ...\n";

You cannot have a string on one line that doesn't end with a ", but two properly terminated strings in a row are concatenated. So "foo" "bar" becomes "foobar". Having "foo" and "bar" on separate lines is fine.

As others have mentioned, C++11 supports raw string literals, which do allow strings to be spread out over multiple lines, and avoids having to write \n:

std::cout << R"(
Choose the operation you want to perform:
1. To insert a node in the BST and in the tree
2. ...
)";
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31
  • You're missing an `<<` between your strings. –  Aug 18 '19 at 20:37
  • 10
    @Chipster No, [they're not](https://softwareengineering.stackexchange.com/q/254984/30872). – GSerg Aug 18 '19 at 20:38
  • @GSerg Bizarre, I could have sworn I've **never** been able to compile something like that before, but now I can't seem to create an example of it. –  Aug 19 '19 at 13:56
0

You can either use a multiline string literal like

const char* s1 = "\nChoose the operation you want to perform:\n"
                     "1. To insert a node in the BST and in the tree\n"
                     "2. some text here";

our you can use raw string literals without any quotes or new line literals (cf, for example, string literals at cppreference.com):

const char* s1 = R"foo(
Choose the operation you want to perform:
1. To insert a node in the BST and in the tree
2. some text here)foo";

These two s1-variants are equivalent. Then write

std::cout << s1;
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • 1
    You're missing the `\\` for line continuation in your 1st example. – πάντα ῥεῖ Aug 18 '19 at 21:02
  • @πάντα ῥεῖ: I don't think so: String literals placed side-by-side are concatenated at translation phase 6 (after the preprocessor). That is, "Hello," " world!" yields the (single) string "Hello, world!" – Stephan Lechner Aug 19 '19 at 20:46
0

You have the option:

std::cout << "\nSome text,\n"
             "\nsomething else\n";

(which was originally proposed by @G. Sliepen)

I would rather prefer using std::endl.

The code would look like:

std::cout << std::endl << "Some text," << std::endl <<
             std::endl << "something else" << std::endl;

Yet another option is to use R prefix (which is used to avoid escaping of any character):

std::cout << R"(
Some text,
something else

)";

My favourite is the last one.

Nestor
  • 687
  • 5
  • 12
  • 4
    It's a common misconception that `std::endl` is more cross-platform. It is in fact equivalent to `"\n"` plus a flush of the stream, which makes it bad for performance. See: https://stackoverflow.com/questions/213907/c-stdendl-vs-n – G. Sliepen Aug 18 '19 at 20:47
  • @G.Sliepen Thank you! I'm wrong about it. I'll edit the answer so as not to delude others. – Nestor Aug 18 '19 at 20:50