0

I am trying to use toupper to convert first letter to uppercase in string but it keeps showing error code below:

no instance of overloaded function"toupper"matches the argument list

Code:

#include<iostream>
#include<cstring>
#include<cctype>
using namespace std ;

int main()
{
    string s("some string");

    if(s.begin() != s.end()){
        auto c = s.begin();
        c = toupper (c);
    }
    return 0 ;
}
NutCracker
  • 11,485
  • 4
  • 44
  • 68
  • 1
    `c` is an iterator pointing to the first character, not a char. you need to dereference it first – perivesta Dec 10 '20 at 08:48
  • `s.begin()` is not a character, it's an iterator that can be dereferenced into a character, so do `auto c = toupper(*s.begin());` – Alexey S. Larionov Dec 10 '20 at 08:49
  • In addition to what is said already, note that here, you are modifyng `c`, not the string itself. You may use `s[0]=toupper(s[0]);` – Damien Dec 10 '20 at 08:51
  • Use `front` not `begin`, and as stated above, use a reference to change the string itself instead of the variable, i.e. `auto& c = s.front();` – john Dec 10 '20 at 08:54
  • This is C++, please use `` instead or `` – JHBonarius Dec 10 '20 at 08:55

1 Answers1

1

Dereferencing the iterator with * operator will do the work:

*c = toupper (*c);
NutCracker
  • 11,485
  • 4
  • 44
  • 68