I'm working on an exercise to store words in a <vector>
of string
s, then convert all the letters to uppercase, and print out eight words per line. Everything works fine except the toupper()
part of my code. Here it all is:
vector<string> words;
string theWords;
string word;
while(cin >> word)
words.push_back(word);
for(auto &i : words) {
word = i;
for(auto &j: word)
j = toupper(j);
}
int k = 0;
for(auto i : words) {
cout << i << " ";
++k;
if(k % 8 == 0)
cout << endl;
}
You're storing your new updated string in word
, but you should be updating i
Change this
for(auto &i : words) {
word = i;
for(auto &j: word) // word is updated, but your vector is not
j = toupper(j);
}
... to this:
for (auto &i : words) // for every string i in words vector
for (auto &j : i) // update your i, not word
j = toupper(j);