c++stringtextasciiextended-ascii

Cant work with characters like á à ã ă â é è ê


My code is supposed to clear any character that isn't a-z or A-Z. For other characters, for instance á à ã ă â é è ê if I can make it work, I'll make them change from á to a and è to e etc.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int counter=0;
    string* word = new string[1];
    string b ="áhelloá";//nothing

    word[0] = "áapple_.Dogá.";//doesnt work
    //word[0] = "apple_.Dog.";//if there is no characters like á it works
    cout<<endl<<word[0].length()<<endl;

    for (int i = 0; i < word[0].length(); ++i)
    {
        if(word[0][i] >= 'A' && word[0][i] <='Z' || word[0][i] >= 'a' && word[0][i] <='z')
        {
            cout<<"Current: "<<word[0][i]<<endl;//shows what characters passed if
        }
        else
        {
            cout<<"Erased: "<<word[0][i]<<endl;//shows what was erased
            word[0].erase(i,1);//deletes char
            i--;
        }
    }

    cout<<endl<<word[0];//prints final word,after erase

    return 0;
}

If I run my code with for example á in Clion it doesn't do anything and returns 0. I tested the same on Repl.it and it somewhat works as intended, I think. Is there a problem with my Clion? What am I doing wrong?


Solution

  • You can use wcout and wstring to deal with Unicode character in C++ within Windows:

    #include <iostream>
    #include <string>
    #include <io.h>
    #include <fcntl.h>
    using namespace std;
    
    int main()
    {
        _setmode(_fileno(stdout), _O_U16TEXT); //set the mode of the output file handle to take only UTF-16 data
        int counter=0;
        wstring word;
    
        word = L"áapple_.Dogá.";
        cout<<'\n'<<word.length()<<'\n';
    
        for (int i = 0; i < word.length(); ++i)
        {
            if(word[i] >= 'A' && word[i] <='Z' || word[i] >= 'a' && word[i] <='z')
            {
                wcout<<"Current: "<<word[i]<<'\n';
            }
            else
            {
                wcout<<"Erased: "<<word[i]<<'\n';
                word.erase(i,1);
                i--;
            }
        }
    
        wcout<<'\n'<<word;
        return 0;
    }
    

    Result :

    Erased: á
    Current: a
    Current: p
    Current: p
    Current: l
    Current: e
    Erased: _
    Erased: .
    Current: D
    Current: o
    Current: g
    Erased: á
    Erased: .
    
    appleDog
    

    For the question "Why it works on repl.it?":

    It should be noted that different compiler and platform handles Unicode character very differently. Quoting @bames53 :

    #include <iostream>
    
    int main() {
        std::cout << "Hello, ф or \u0444!\n"; }
    

    This program does not require that 'ф' can be represented in a single char. On OS X and most any modern Linux install this will work just fine, because the source, execution, and console encodings will all be UTF-8 (which supports all Unicode characters).

    Things are harder with Windows and there are different possibilities with different tradeoffs.

    By the way, IMO you're using dynamic array for no reason. A wstring is enough.

    Also see