tags:

views:

1124

answers:

5

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.

+7  A: 
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";
Vulcan Eager
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
Jonathan Leffler
+5  A: 
cout << "Error:This is a really long error message "
    "that does not exceed the maximum permitted length.\n";
laalto
+9  A: 

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.

Thomas
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"
Hasturkun
Concatenated by the compiler, not during runtime.
emddudley
A: 

This will work on all the C++, weather it's VS, or on Linux

cout<<"Error:This is a really long error message that \
    exceeds the maximum permitted length.\n";
UBpine Inc
You are embedding extra tabs or spaces in the string.not a good idea.
Hasturkun
+1  A: 

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.

corlettk
The 80 character limit has historical roots, but is mainly kept because the human eye is better at keeping track of short lines.
bayer