name2 += toupper(name2[i]); You are appending it to the end. Shouldn't this be name2[i] = toupper(name2[i]); PS when you append you increment the size so you have an infinite loop as mentioned by LB
The second code probably has undefined output since upStr is undefined. The first will work fine or initalize upStr to an empty string. Maybe you hit run and didn't compile first because end should be endl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
usingnamespace std;
void capitalize(string& name2) {
for (short i = 0; i < name2.size(); ++i)
name2[i] = toupper(name2[i]);
}
int main() {
string name2 = "brenda";
capitalize(name2);
cout << name2 << endl;
}