c++ - 循环为 vector<struct> 元素生成大写字符

标签 c++ vector loops

调试后我认为我做对了。但它只是作为 vector 中第一个元素的第一个成员被更正了。

while ( !inFile->eof() )
{
    getline( *inFile, str1, ',' );      
    sStruct.str1 = str1;
    getline( *inFile, str2, ',' );
    sStruct.str2 = str2;
    getline( *inFile, str3, ',' );
    sStruct.str3 = atof( str3.c_str() );
    getline( *inFile, str4 );
    sStruct.str4 = atof( str4.c_str() );

    myLength = sStruct.str1.length();

    for( ; sIndex < myLength; sIndex++ )
    { 
        if ( 97 <= str4[sIndex] && str4[sIndex] <= 122 )
        {
            str4[sIndex] -= 32;
        }
    }   

    sStruct.str1 = str1;
    vectorData->push_back( sStruct );
}

在我选择读取文件的方法下执行此代码,仅将第一个结构成员(在本例中为 str1)更改为全部大写字符。对于同一结构成员 str1,所有字符均不受影响。

我的循环没有做什么?

最佳答案

如评论中所述,您应该首先在代码中查看以下几点:

  • 在进入此 while 循环之前,sIndex 的值是多少?
  • 为什么在 for 循环中使用 str4[],但获取长度并在 str1 上进行赋值?
  • 您真的确定只有 str1 被更改为大写吗?给定的代码不会那样做...
  • 不要自己进行大写计算,而是使用 toupper()方法代替

关于c++ - 循环为 vector<struct> 元素生成大写字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/645578/

相关文章:

C++ std::vector std::sort 无限循环

C++/数学GL : Missunderstanding of vector fields

haskell - 未装箱向量中没有与 unsafeUpdate_ 进行流融合

C++试图交换 vector 中的值

c++ - 尝试使用gcc构建pybind11项目时获取 undefined reference

c++ - 有人可以解释为什么 int 会根据系统架构呈现不同的大小吗?

c++ - 使用 C++0x : call to deleted constructor of 时的 clang++ 错误消息

javascript - 第一次在 javascript/jquery 中运行函数时没有做任何事情

JavaScript - 将数组转换为列表

C++ 如何检查数组中的所有值是否不同?