开发者

press 'Enter' in the middle of writing a string in c++

开发者 https://www.devze.com 2023-03-08 06:58 出处:网络
I want to write a string and press \"Enter\" while continuing writing this string, without having to close the \"\".

I want to write a string and press "Enter" while continuing writing this string, without having to close the "".

For example, i want to write the following line:

string drawing = "\\n\\n" +  
"W     W      W        " +  
"\\nW        W  W     W  "  + 开发者_开发百科 
"\\n              '.  W  ";  

but when i tried to do so, the compiler complains this:

error: invalid operands of types ‘const char [5]’ and ‘const char [23]’
to binary ‘operator+’

Is there a nice way to enter a new line without concating the string (like there is in python, for example, with entering the '\')?

Thanks.


Try it without "+".

string drawing = "\n\n"
"W W W "
"\nW W W W "
"\n '. W ";


You end the string with a backslash:

std::string drawing = "\n\n\
W W W \
\nW W W W \
\n '. W ";


In C++, when you have two or more string literals only separated by whitespace, they get concatenated. So you can use

string drawing = "\n\n"
"W W W "
"\nW W W W "
"\n '. W ";
0

精彩评论

暂无评论...
验证码 换一张
取 消