读书人

运行出现异常

发布时间: 2012-08-30 09:55:54 作者: rapoo

运行出现错误
请教,哪里错了。
#include<iostream>
#include<string>
using namespace std;
int main(){
string text;
const string sep=",.\"\n ";
const int max_word=100;
string word[max_word]={0};
cout<<"请输入一段话(以#结束):";
getline(cin,text,'#');
int start=text.find_first_not_of (sep);
int end=0;
int word_count=0;
while(start!=string::npos&&word_count<max_word)
{
end=text.find_first_of(sep,start+1);
if(end==string::npos)
end=text.length();
word[word_count]=text.substr(start,end-start);
start=text.find_first_not_of(sep,end+1);
word_count++;
}
for(int i=0;i<=word_count;i++)
cout<<word[i]<<endl;
return 0;
}

[解决办法]
如果是char数组的话 可以这样赋值 char w[max_word]={0};


[解决办法]
0是int string的构造函数没有用一个int 或者一个 char类型的变量来进行构造的。
换成{""}之类的可以。

读书人网 >C++

热点推荐