Search code examples
c++visual-c++toupper

toupper() not working in a for range loop


Can anyone shed any light on this not working? I tested if it was directly referencing the char correctly by changing the toupper() expression to an expression that made every character an 'X' and that worked so I have no idea what's going wrong.

 for (decltype(words.size()) i = 0; i < words.size(); ++i) {
    for (auto &u : words[i])
        toupper(u);
    if ((i % 8) != 0)
        cout << words[i] << ' ';
    else
        cout << endl << words[i] << ' ';
}

Solution

  • It is because you are discarding what is returned by toupper().

    To save the converted characters, change

    toupper(u);
    

    to

    u = toupper(u);