0

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] << ' ';
}
Ezra Goss
  • 124
  • 8

2 Answers2

3

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

To save the converted characters, change

toupper(u);

to

u = toupper(u);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

toupper signature is int toupper(int c), not void toupper(char& c). It returns the modified value, it doesn't alter anything by reference.

Maybe you wanted to do something like

std::transform(words[i].begin(), words[i].end(), words[i].begin(), [](char c) { return std::toupper(c); });
Jack
  • 131,802
  • 30
  • 241
  • 343
  • the reference is defined in the for range loop... I might not be fully understanding your answer correctly but the answer provided by MikeCAT worked which leads me to believe toupper() is perfectly equipped to handle a reference variable – Ezra Goss Jul 10 '16 at 01:46
  • toupper takes the parameter by value, not by reference, there's no chance it's able to modify the original value. That's why you need to reassign the return value like in MikeCAT answer. I suggested you a way to do with `std::transform` without the need of manual iteration. – Jack Jul 10 '16 at 01:59
  • Oh I understand what you're referring to I just misread your answer. Right, thank you again, I wasn't intending to have toupper() do the work but had I been this would have helped me a lot. – Ezra Goss Jul 10 '16 at 03:28