Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

split

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.

like image 684
Meir Avatar asked Jun 09 '09 11:06

Meir


People also ask

Can you break lines in C?

It's called "newline". You can't put it directly in the string because that would create a new line in the source code, but inside quotes in C you can produce it with \n . Alternatively, instead of printf you could use puts , which prints a new line after the string.

How do you split lines in code?

Use the line-continuation character, which is an underscore ( _ ), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.

How do you split a line of code in C++?

The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier. This is line one. This is line two. This is line one.

How do I write a multi line string literal in C?

We can use string literal concatenation. Multiple string literals in a row are joined together: char* my_str = "Here is the first line." "Here is the second line."; But wait!


2 Answers

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.

like image 127
Thomas Avatar answered Sep 30 '22 17:09

Thomas


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"; 
like image 39
Agnel Kurian Avatar answered Sep 30 '22 16:09

Agnel Kurian