这个程序为什么会崩溃?

hixeek 2012-03-15 02:20:05
这个程序将每个输入到vector对象中的字符串都转换为大写字母并输出,可是为什么我输完并且ctrl+z回车以后程序就会崩溃呢?

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main()
{
vector<string> strvec;
string str;
while (cin >> str)
{
strvec.push_back(str);
}
if (strvec.size() == 0)
{
cout << "No string?!" << endl;
return -1;
}
for (vector<string>::size_type ix; ix != strvec.size(); ++ix)
{
for (string::size_type i = 0; i != strvec[ix].size(); ++i)
{
strvec[ix][i] = toupper(strvec[ix][i]);
cout << strvec[ix] << " ";
if ((ix + 1) % 8 == 0)
cout << endl;
}
}
return 0;
}

...全文
100 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
LittleCommit 2012-03-15
  • 打赏
  • 举报
回复
vector<string>::size_type ix;ix没有初始化。
应该这样写:
for (vector<string>::size_type ix = 0; ix != strvec.size(); ++ix)
{
for (string::size_type i = 0; i != strvec[ix].size(); ++i)
{
strvec[ix][i] = toupper(strvec[ix][i]);
cout << strvec[ix] << " ";
if ((ix + 1) % 8 == 0)
cout << endl;
}
}
vanxeger 2012-03-15
  • 打赏
  • 举报
回复
你这循环有问题啊!
elegant87 2012-03-15
  • 打赏
  • 举报
回复

for (vector<string>::size_type ix; ix != strvec.size(); ++ix) //ix没有初始化
//改为
for (vector<string>::size_type ix=0; ix != strvec.size(); ++ix)
面包大师 2012-03-15
  • 打赏
  • 举报
回复
for (vector<string>::size_type ix=0; ix != strvec.size(); ++ix)//ix初始值要为0
面包大师 2012-03-15
  • 打赏
  • 举报
回复
for (vector<string>::size_type ix=0; ix != strvec.size(); ++ix)

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧