2014-02-07 53 views
0

这是一个常数,我使用,它被分成这样的,因为我不希望有滚动我的编辑你可以在字符串文字中插入中断吗?

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

您是否尝试过使用'\ n'插入换行符? –

回答

8

只需插入\n字符来强制一个新行

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

谢谢你这个作品。我曾尝试添加/ n,但是我的“n”之后有一个空格,然后使其出现在字符串 – Jessica

+0

中。线索,想象它是Windows操作系统并创建表单应用程序,而不是控制台。 – ST3

0

添加\n您要行打破的字符串中。

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."; 
3

在C++ 11,可以使用原始字符串字面

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

个输出:

this string 
is for real 

(我把这个答案在这里为迂腐的原因,可使用\ n)

0

只需插入一个换行符“\ 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."; 

对不对?

0

好的答案是好的,但我的提议是使用\r\n换新线。请再阅读here,这两个符号应该始终有效(除非您使用的是Atari 8位操作系统)。

还有一些解释。

  • \n - 换行。这应该将打印指针移动一行较低,但它可能会或可能不会将打印指针设置为开始行。
  • \r - 回车。这会在行的开始处设置行指针,并且可能会或可能不会更改行。

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

相关问题