首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这个程序为什么会崩溃?解决方法

2012-03-29 
这个程序为什么会崩溃?这个程序将每个输入到vector对象中的字符串都转换为大写字母并输出,可是为什么我输

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

C/C++ code
#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;}



[解决办法]
C/C++ code
for (vector<string>::size_type ix=0; ix != strvec.size(); ++ix)//ix初始值要为0
[解决办法]
C/C++ code
   for (vector<string>::size_type ix; ix != strvec.size(); ++ix)  //ix没有初始化   //改为   for (vector<string>::size_type ix=0; ix != strvec.size(); ++ix)
[解决办法]
你这循环有问题啊!
[解决办法]
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;
}
}

热点排行