Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #82859

Re: Hello World in Russian

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c++
Subject Re: Hello World in Russian
Date 2022-01-28 23:27 -0500
Organization A noiseless patient Spider
Message-ID <st2fmr$p6q$1@dont-email.me> (permalink)
References <i6GdnUGdpJgzJGn8nZ2dnUU7-KnNnZ2d@giganews.com>

Show all headers | View raw


On 1/28/22 22:51, Joseph Hesse wrote:
> In the following program, I have written "hello world", copied from
> the iternet, in Russian. I don't understand Russian but I understand
> their alphabet is different from the English alphabet. The program is
> supposed to output the original message and the characters in the
> original message. My question is why are they different?
> Thank you,
> Joe
>
> $ more Hello.cpp
> #include <iostream>
> using namespace std;
>
> int main()
> {
> const wchar_t russian[] = L"Привет мир"; //Russian for "hello, world"

I do understand Russian (a little), and that looks correct to me.

> wcout << russian << endl;
>
> for(const wchar_t &x : russian)
> wcout << x << L", ";
> wcout << endl;
> }
> $ g++ -std=c++14 Hello.cpp
> $ ./a.out
> Privet mir <== original message
> P, r, i, v, e, t, , m, i, r, , <== wchars in original message
> $

The problem is that the behavior of your program depends upon the
locale. The default locale is the "C" locale, which doesn't support
those Russian characters. How those characters are interpreted is
implementation-defined - I think it's amusing that they get
misinterpreted as latin letters with the same sounds.

I added two lines to your program:

#include <clocale>

and as the first line of main():

std::setlocale(LC_CTYPE, "");

"" is the name of the native locale, what that means is up to your
implementation. On my desktop machine, it's the locale identified by the
LANG environment variable. I've currently got that set to en_US.UTF-8.
Because that locale supports UTF-8, it in particular supports those
Russian letters, and I get the following output:

Привет мир
П, р, и, в, е, т, , м, и, р, ,

You'll need to determine how the native locale is set on your machine,
and what the list of supported locales is, and they you'll have to find
out which one(s) support the characters you want to print.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Hello World in Russian Joseph Hesse <joeh@gmail.com> - 2022-01-29 05:51 +0200
  Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-28 23:27 -0500
    Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-29 10:21 +0000
  Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-28 22:42 -0800
    Re: Hello World in Russian David Brown <david.brown@hesbynett.no> - 2022-01-29 13:01 +0100
      Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-29 14:12 +0100
        Re: Hello World in Russian David Brown <david.brown@hesbynett.no> - 2022-01-29 17:34 +0100
      Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-30 14:17 -0500
        Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-30 20:17 -0800
  Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-29 11:48 +0100
    Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-29 17:39 +0200
      Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 09:18 +0100
        Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 12:12 +0000
          Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 14:02 +0100
            Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 14:45 +0000
              Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-30 21:53 +0100
                Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-30 21:00 +0000
            Re: Hello World in Russian James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-30 14:02 -0500
          Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 10:23 +0000
            Re: Hello World in Russian scott@slp53.sl.home (Scott Lurndal) - 2022-01-31 16:11 +0000
              Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 16:36 +0000
              Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-31 20:20 +0200
            Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-31 16:12 +0000
              Re: Hello World in Russian Muttley@dastardlyhq.com - 2022-01-31 16:39 +0000
                Re: Hello World in Russian Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-31 23:47 +0000
        Re: Hello World in Russian Manfred <noname@add.invalid> - 2022-01-31 16:45 +0100
        Re: Hello World in Russian Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-01-31 10:10 -0800
          Re: Hello World in Russian "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-01-31 19:25 +0100
      Re: Hello World in Russian Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-01-30 12:09 +0100
        Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-30 14:26 +0200
          Re: Hello World in Russian Richard Damon <Richard@Damon-Family.org> - 2022-01-30 08:59 -0500
            Re: Hello World in Russian Paavo Helde <eesnimi@osa.pri.ee> - 2022-01-30 18:21 +0200
              Re: Hello World in Russian Richard Damon <Richard@Damon-Family.org> - 2022-01-30 12:56 -0500
                Re: Hello World in Russian Vir Campestris <vir.campestris@invalid.invalid> - 2022-02-07 21:05 +0000

csiph-web