我一直在尝试在连接的字符串上编写此代码:
#include<iostream>
#include<string>
using namespace std;
int main (){
string line, total;
//Read strings from the input//
cout<<"Enter a String"<< endl;
while(getline(cin,line)){
cout<< "Enter another String"<< endl;
if(!line.empty())
total+=line;
cout<<" Concatenated string is\t"<<total<< endl;
}
return 0;
}
但是,我的输出在两个附加字符串之间不存在空格。我还需要一种方法来终止添加的字符串。
+=
不会自动在两个字符串之间添加空格。您需要手动添加它。
像这样的事情:
total += " " + line;
另外,本文还提供了有关 += 运算符的示例说明。http://www.cplusplus.com/reference/string/string/operator+=/ http://www.cplusplus.com/reference/string/string/operator+=/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)