返回信息流#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string st,s;
string::size_type i=0;
cin>>st;
for(string::size_type ix=0;ix!=st.size();++ix)
{
if(!ispunct(st[ix]))
{
s[i]=st[ix];
i++;
}
}
cout<<s<<endl;
return 0;
}
程序的原意是输入一个字符串,将其中的标点去掉然后输出
我输入he,ta.
调试运行到第一个for循环完成的时候,s的值为什么会变成图中所显示的乱码?
然后for走第二个循环的时候,程序就出错中断了
这是一条镜像帖。来源:北邮人论坛 / cpp / #29209同步于 2009/9/27
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[求助]一个关于string的程序错误
tt82238971
2009/9/27镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
把声明的变量都初始化一下...
【 在 tt82238971 (可以是中文) 的大作中提到: 】
: #include <iostream>
: #include <string>
: #include <cctype>
: ...................
【 在 wangfuwei 的大作中提到: 】
: lz也初学?看C++ Primer?看得不够细
: string s没有初始化,是空串,怎么能够下标操作呢?
被你看出来了。。
难道你也在看C++Primer?
谢谢解答哈