有没有高手帮小弟我看看,小弟我写的这个去掉字符串中标点的小程序,为什么不能运行了,运行环境vc++6.0
有没有高手帮我看看,我写的这个去掉字符串中标点的小程序,为什么不能运行了,运行环境vc++6.0
[i][/i]有没有高手帮我看看,我写的这个去掉字符串中标点的小程序,为什么不能运行了,运行环境vc++6.0,运行到if (ispunct(line[i])==true)这里就报错unhandle exception,access violation
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{ string line;
string result_s;
int i;
int n;
cout<<"enter the string:"<<endl;
getline(cin,line);
for(i=0;i<=line.size();i++)
{
if (ispunct(line[i])==true)
{cout<<"punctuation occur"<<endl;}
else
{result_s[n]=line[i];n++;}
}
cout<<result_s<<endl;
return 0;
}
------解决方案--------------------
[i][/i]有没有高手帮我看看,我写的这个去掉字符串中标点的小程序,为什么不能运行了,运行环境vc++6.0,运行到if (ispunct(line[i])==true)这里就报错unhandle exception,access violation
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{ string line;
string result_s;
int i;
int n;
cout<<"enter the string:"<<endl;
getline(cin,line);
for(i=0;i<=line.size();i++)
{
if (ispunct(line[i])==true)
{cout<<"punctuation occur"<<endl;}
else
{result_s[n]=line[i];n++;}
}
cout<<result_s<<endl;
return 0;
}
------解决方案--------------------
- C/C++ code
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string line; string result_s; cout<<"enter the string:"<<endl; getline(cin,line); for(int i=0; i<line.size(); i++) { if (ispunct(line[i])) cout<<"punctuation occur"<<endl; else result_s.push_back(line[i]); } cout<<result_s<<endl; return 0; }
------解决方案--------------------
到if (ispunct(line[i])==true)这里就报错unhandle exception,access violation
从for(i=0;i<=line.size();i++)可以看出i越界了,元素有line.size()个,
但是下标最多是line.size()-1。
------解决方案--------------------
VC6里没有push_back吗?太老了不记得了,那就这样吧:
- C/C++ code
result_s += line[i];