读书人

新手此段字符串异常原因

发布时间: 2013-03-22 09:49:50 作者: rapoo

新手求助:此段字符串错误原因
为什么将输入的字符串连接,并用空格隔开debug错误?
#include<iostream>
#include<string>
using std::string;
int main()
{
string result,str;
std::cout<<"Enter string(Ctl+Z to end):"<<std::endl;
while(std::cin>>str)
result=result+''+str;----这里有什么问题?
std::cout<<"String equal to the concatenation of these string is:"<<std::endl<<result<<std::endl;
return 0;
}

[解决办法]
是空字符串,不是字符
改为:


#include<iostream>
#include<string>
using std::string;
int main()
{
string result,str;
std::cout<<"Enter string(Ctl+Z to end):"<<std::endl;
while(std::cin>>str)
result=result+" "+str;//----这里有什么问题?
std::cout<<"String equal to the concatenation of these string is:"<<std::endl<<result<<std::endl;
return 0;
}

读书人网 >C++

热点推荐