How to split long lines of code in c++?

88,397

Solution 1

Two options:

cout << "Error:This is a really long "
 << "error message that exceeds "
 << "the maximum permitted length.\n";

Or:

cout << "Error:This is a really long "
    "error message that exceeds "
    "the maximum permitted length.\n";

The second one is more efficient.

Solution 2

cout<<"Error:This is a really long error "
"message that exceeds the maximum permitted length.\n";

or

cout<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

or

c\
o\
u\
t<<"Error:This is a really long error \
message that exceeds the maximum permitted length.\n";

Solution 3

Just my 2 bobs worth...

I wouldn't wrap that line of code. I'd leave it as one big long string.

The 80 character convention was based on the limitations of the machinery of the day. Terminals where typically 80 by 32 characters. Cheap dot-matrix printers + continious-sheet paper was 80 characters. Only the rich people could afford a 132 character setup. And guess what... those who could afford it wrapped code at 132 characters, which dramatically decreases the number of lines which must be wrapped, and produces "cleaner" source code.

Those constraints don't apply today. My text editor displays 150 columns by 52 lines of 10pt courier new. My work monitors would display something like 400 by 65 (I've never tested it). I haven't printed a single line of source code in years... and the last time I did so was so that I could read it one the bus on the way home, when my laptop was on the fritz.

Modern langues are much more verbose than "older style" languages... and this is good. If you called anything a BeanContextServicesSupport.BCSSServiceProvider in Pascal your boss would have told you to go sit in the corner. Pascal identifiers where only significant to 8 characters!

So why persist with this outmoded and (to me) annoying convention? It makes very little practical sense.

So... I wrap "code lines" at 132 characters. I don't bother to wrap "text lines" at all.

See also: The width of two horses arses!

Cheers. Keith.

Solution 4

cout << "Error:This is a really long error message "
    "that does not exceed the maximum permitted length.\n";
Share:
88,397
Meir
Author by

Meir

Updated on October 23, 2020

Comments

  • Meir
    Meir over 3 years

    I need to make sure none of the lines in my code exceeds a a certain length.

    Normally I separate lines where there's a comma or another suitable break.

    How can I separate this line into 2?

    cout<<"Error:This is a really long error message that exceeds the maximum permitted length.\n";
    

    If I just press enter somewhere in the middle it doesn't work.

  • Hasturkun
    Hasturkun about 15 years
    You are embedding extra tabs or spaces in the string. not a good idea.
  • Hasturkun
    Hasturkun about 15 years
    An explanation of the second case would be handy: ANSI C allows string literals to be concatenated, as in "foo" "bar" "baz" would be the same as "foobarbaz"
  • Jonathan Leffler
    Jonathan Leffler about 15 years
    With a strong recommendation to avoid the variants with a backslash at the end of the line. You didn't take the chance to show off comment start and end symbols split over multiple lines by backslash-newline combinations. :D
  • bayer
    bayer about 15 years
    The 80 character limit has historical roots, but is mainly kept because the human eye is better at keeping track of short lines.
  • sourcenouveau
    sourcenouveau almost 15 years
    Concatenated by the compiler, not during runtime.
  • mikemaccana
    mikemaccana almost 9 years
    @bayer that's correct, but enforcing presentation layer for a single output device is a poorer solution than allowing output devices to determine a line length based.
  • TextGeek
    TextGeek over 6 years
    BTW, the "two horses arses" story is a myth (though quite amusing) -- see for example snopes.com/history/american/gauge.asp; or just consider that railroad gauges vary widely (check out the Boston subway system!)
  • not2qubit
    not2qubit over 3 years
    This is still an important comment as it's often overlooked by others. Noting that hidden tabs and extra alignment spaces would mess up your formatting wither in code or as compiled result.