skip to Main Content

I’m trying to parse a string character per character so I can load an image depending on every letter. So if the text is "Hello" i will print 5 images that are the same letters but made in photoshop.
It works fine until I want to parse the € symbol.

std::string al = "Test €";

std::string letter="";
for (int i=0; i< al.length();++i)
{
    if (al[i]=='.') letter ="dot";
    else if (al[i]==',') letter ="coma";
    else if (al[i]==' ') letter ="space";
    //else if (al[i]=='€') letter ="euro";
    else letter=al[i];
}

This works fine: letter will adquire the values:"T","e","s","t","space" but if I uncomment the else if (al[i]=='€') letter ="euro"; and try to build it, then I receive a red mesage error that says:

warning: multi-character character constant

So the thing is that I need to know if al[i] is the € symbol to be able to assing "euro" to letter (then my code will be able to work with it)

I’ve search on google and found that link which says that "u20AC" is the c++ code for the € and I suppose that the symbol needs more than a byte maybe, but still can’t find how to deal with it and be able to parse it in my code. Any Idea of how could I do it?

Thank you so much.

Note: I don’t know the C++ version used (dunno where I can check it) but I know its not c++11

3

Answers


  1. std::string assumes all characters are encoded in one byte. The symbol you want is a unicode character that’s encoded in two bytes (that’s why you get ‘multi character character’ error)

    Best thing to use a library that understand unicode and stick with that one. This question might be relevant: unicode string in c++ with boost

    Login or Signup to reply.
  2. The first issue is you should be mindful of using Unicode characters in your source code. Compilers are only required to support a specific character set and not all compilers may like your code. I suggest you read this answer for a more detailed explanation.

    Second problem is that the character is to large to be represented in a character literal. You need to explicitly tell the compiler to use a wide character literal instead.

    L'x20AC`   // Notice the preceeding L
    

    The third problem is the rest of your code still uses narrow character strings. Change std::string to std::wstring.

    Login or Signup to reply.
  3. “u20AC” is a string, so you should split your big string into some sub strings
    then you compare with them. If they are equal, then you replace it
    else you replace per character in the sub strings

    string al = "Test €";  (assume you declared std namespace already) 
    string letter="";
    char* ch = strtok(al," ");
    
    while(ch!=NULL) {
        if(al.compare(ch)==0){ 
            letter="euro";
        }
        //your code here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search