0

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?

silverfox
  • 1,568
  • 10
  • 27
Jesepy
  • 37
  • 6
  • 1
    It's because [C++ doesn't really do Unicode](https://stackoverflow.com/a/31475700/10077). – Fred Larson Jun 09 '21 at 13:23
  • 1
    `char` isn't capable of representing character that aren't ASCII. You need utf-8 or other encoding schemes to deal with characters. You need a different that isn't `std::string`. – ALX23z Jun 09 '21 at 13:24
  • @FredLarson Why it works on Replit?I checked some post about unicode but it was getting kind of complicated for my and i didnt have some libraries too. – Jesepy Jun 09 '21 at 13:24
  • @ALX23z i found wcout,wstring if thats what you mean but idk how to make them work – Jesepy Jun 09 '21 at 13:26
  • 1
    You'll have to use `std::wstring`, `std::wcout` and `L"abcdef"`. Also: where tf did you learn c++ from?! Why are you using dynamic arrays for no reason?! Also using namespace std and endl! – Nikita Demodov Jun 09 '21 at 13:31
  • 1
    @Jesepy also `std::string` might suffice on some OS's(e.g. it works on my mac) but not on others. – Nikita Demodov Jun 09 '21 at 13:33

1 Answers1

2

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

silverfox
  • 1,568
  • 10
  • 27
  • I had to change _O_U16TEXT to 0x00020000 because it said undeclared in this scope,why was that.I think it works great,thanks – Jesepy Jun 09 '21 at 14:25
  • 1
    @Jesepy It's probably not available in your compiler. The workaround is in this [post](https://stackoverflow.com/questions/8871540/o-wtext-o-u16text-o-u8text-are-these-modes-possible-in-mingw-compiler-ar). – silverfox Jun 09 '21 at 14:29
  • yep thats where i found it – Jesepy Jun 09 '21 at 14:30