0

So, I've been doing Reddit's daily programmer #140 and can't use std::toupper and std::erase.

Includes:

#include <iostream>
#include <string>
#include <cctype>

Part with toupper and erase (used to transform words to 'CamelCase'):

std::string tekst;
std::cin >> tekst;    

tekst[0] = std::touppper(tekst[0]);
for(unsigned int i = 0; i < tekst.size(); i++){
    if(tekst[i] == 32){
        std::erase(tekst[i], 1);
        tekst[i] = std::toupper(tekst[i]);
    }
}

And compiler shows errors:

error: 'touppper' is not a member of 'std'
error: 'erase' is not a member of 'std'

What can cause it? Thanks in advance!

Demiu
  • 97
  • 5

3 Answers3

4

Not

std::touppper

but

std::toupper

You need to pass a locale to the function, see for example: http://www.cplusplus.com/reference/locale/toupper/

piokuc
  • 25,594
  • 11
  • 72
  • 102
1

std::touppper does not exist, as it is spelled with two p's, not with three :). std::erase is not a standard function, check this: Help me understand std::erase

Community
  • 1
  • 1
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
0

You probaly want to use std::toupper() as the basis of your implementation. Note, however, that std::toupper() takes its argument as int and requires that the argument is a positive value of EOF. Passing negative values to the one argument version of std::toupper() will result in undefined behavior. On platforms where char is signed you will easily get negative values, e.g., when using ISO-Latin-1 encoding with my second name. The canonical approach is to use std::toupper() with the char convert to an unsigned char:

tekstr[0] = std::toupper(static_cast<unsigned char>(tekstr[0]));

With respect to erase() you are probably looking for std::string::erase():

tekstr.erase(i);

Note that if the string ends in a space, you don't want to access the character at index i after blowing the last space away!

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380