为什么是c++;字符串串联缺少空格? 我正在使用C++字符串,是编程初学者。
我期待着:99个红气球 但我收到了:99个红气球 为什么呢为什么是c++;字符串串联缺少空格? 我正在使用C++字符串,是编程初学者。,c++,string,io,C++,String,Io,我期待着:99个红气球 但我收到了:99个红气球 为什么呢 #include <string> #include <iostream> using namespace std; int main() { string text = "9"; string term( "9 "); string info = "Toys"; string color; char hue[4] = {'R','e','d','\0'}; co
#include <string>
#include <iostream>
using namespace std;
int main()
{
string text = "9";
string term( "9 ");
string info = "Toys";
string color;
char hue[4] = {'R','e','d','\0'};
color = hue;
info = "Balloons";
text += (term + color + info);
cout << endl << text << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
字符串text=“9”;
字符串术语(“9”);
string info=“玩具”;
字符串颜色;
字符色调[4]={'R','e','d','\0'};
颜色=色调;
info=“气球”;
文本+=(术语+颜色+信息);
CUT< P>你对代码< Hue>代码>的定义不包括任何空格。(0是C++如何知道字符串的结尾,这不是一个空间。)注意,代码中的<代码>术语< /> >确实有尾随空间。
要修复它,请将色调更改为:
char hue[5] = {'R','e','d',' ','\0'};
或者,在构建最终文本时,在添加内容中包含空格:
text += (term + color + " " + info);
<>你对<代码>色相<代码>的定义不包括任何空格。(0是C++如何知道字符串的结尾,这不是一个空间。)注意,代码中的<代码>术语< /> >确实有尾随空间。
要修复它,请将色调更改为:
char hue[5] = {'R','e','d',' ','\0'};
或者,在构建最终文本时,在添加内容中包含空格:
text += (term + color + " " + info);
这是因为字符串只是一个字符接一个字符,info=“Ballobs”或颜色中没有空格。请注意,“\0”不是空格。
要获得空间,您需要:
text += (term + color + " " + info);
这是因为字符串只是一个字符接一个字符,info=“Ballobs”或颜色中没有空格。请注意,“\0”不是空格。
要获得空间,您需要:
text += (term + color + " " + info);
因为colr末尾或info开头都没有空格。所以您可以尝试:
info = " Balloons";
因为colr末尾或info开头都没有空格。所以您可以尝试:
info = " Balloons";