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.
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.
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.
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.
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!
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.
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With