你能把断点放在字符串文字中吗?

时间:2014-02-07 17:56:33

标签: c++ newline

这是我正在使用的常量,它是这样拆分的,因为我不想滚动我的编辑器

const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, until you reach your goal.";

int main() 
{
    cout << PROGRAM_DESCRIPTION;
    return 0;
}

目前在命令提示符下打印出

Program will calculate the amount accumulated every month you save,until you re
ach your goal.

当打印出来时,我希望它可以在下面两个单独的行上打印...

Program will calculate the amount accumulated every month you save,
until you reach your goal.

我不知道在一个字符串中放置break语句的位置,所以我可以正确打印出来。

6 个答案:

答案 0 :(得分:8)

只需插入\n字符即可强制换行

const string PROGRAM_DESCRIPTION = "Program will calculate the amount "
"accumulated every month you save, \nuntil you reach your goal.";

答案 1 :(得分:4)

您可以在文字的第一部分末尾使用\n,如下所示:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount\n"
"accumulated every month you save, until you reach your goal."; //   ^^

如果您不希望将文字拆分为可读性,则无需将文字拆分为:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount accumulated every month you save,\nuntil you reach your goal.";

答案 2 :(得分:3)

在C ++ 11中,您可以使用原始字符串文字

const char* stuff =
R"foo(this string
is for real)foo";
std::cout << stuff;

输出:

this string
is for real

(我出于学究原因把这个答案放在这里,使用\ n)

答案 3 :(得分:0)

在要断行的字符串中添加\n

答案 4 :(得分:0)

只需在const字符串中的所需位置插入换行符“\ n”,就像在普通文字字符串中一样:

const string PROGRAM_DESCRIPTION = "Program will calculate the amount\naccumulated every month you save, until you reach your goal.";

cout << PROGRAM_DESCRIPTION;

简单。就像我以前使用文字字符串一样:

cout << "Program will calculate the amount\naccumulated every month you save, until you reach your goal.";

右?

答案 5 :(得分:0)

答案很好,但我的建议是使用\r\n换行。再读一下here,这两个符号应始终有效(除非您使用的是Atari 8位操作系统)。

还有一些解释。

  • \n - 换行。这应该将打印指针向下移动一行,但它可能会或可能不会将打印指针设置为开头o行。
  • \r - 回程。这会将行指针设置在开头的o行,可能会也可能不会改变行。

  • \r\n - CR LF。将打印指针移动到下一行并将其设置在行的开头。