this code gives me error message, i am trying to make words in this string in uppercase.

1
2
3
   
string hello="Hello";
cout<<hello.toupper();


string is already included in the library.
There isn't a member function in the string class called 'toupper'. The string is supposed to be an argument to the function, but it's only mean't for one character.

int toupper ( int c );
Lowercase letter character to be converted, casted to an int, or EOF.

You'll have to do something like this.
1
2
3
std::string str = "Hello";
for(int i = 0; str[i] != '\0'; ++i) 
    std::cout << toupper(str[i]);
Last edited on
why doesn't this work, i am not sure how '\0' works. so i tried the following i received an error message which makes sense to me.
1
2
3
4
5
string str="hello";
for(int i=0;i<str.size();++i)
cout<<toupper(str[i]);

i didn't get error message sorry, i got numbers instead. 76292729272 something.
That's because the function returns an integer, or the ASCII value of that character. You'll have to cast it as a char if you want it to work correctly.

1
2
3
4
5
6
7
int main(void) {
    std::string str = "Hello";
    for(int i = 0; i < str.size(); ++i) 
        std::cout << static_cast<char>(toupper(str[i]));
        // std::cout << (char)toupper(str[i]); 
    return 0;
} 
Last edited on
it finally works. thanks, man.
Topic archived. No new replies allowed.