2

When i'm runnig this program:

#include <iostream>
#include "ncurses.h"

using namespace std;

int main() {
    setlocale(LC_ALL, "Russian"); 
    const char *mesg = "Просто строка";
    initscr();
    scrollok(stdscr,TRUE);
    for(int i = 0; i < 10000; i++)
    {
        printw("%s %d \n", mesg, i);
        refresh();
    }
    getch();
    endwin();
    return 0;
}

I have such output:

......
�~_�~@о�~A�~Bо �~A�~B�~@ока 9989
�~_�~@о�~A�~Bо �~A�~B�~@ока 9990
�~_�~@о�~A�~Bо �~A�~B�~@ока 9991
�~_�~@о�~A�~Bо �~A�~B�~@ока 9992
�~_�~@о�~A�~Bо �~A�~B�~@ока 9993
�~_�~@о�~A�~Bо �~A�~B�~@ока 9994
�~_�~@о�~A�~Bо �~A�~B�~@ока 9995
�~_�~@о�~A�~Bо �~A�~B�~@ока 9996
�~_�~@о�~A�~Bо �~A�~B�~@ока 9997
�~_�~@о�~A�~Bо �~A�~B�~@ока 9998
�~_�~@о�~A�~Bо �~A�~B�~@ока 9999

I'm compiling this way: g++ main.cpp -o main -lncurses

How can I fix it? I have searched in the internet, but there is no solution.

I have tried all variants of setlocale();

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 1
    how about using wchar_t instead of char? – Leśny Rumcajs Dec 09 '15 at 11:12
  • What O/S are you using? Linux? – trojanfoe Dec 09 '15 at 11:13
  • 2
    @trojanfoe yes, Linux, especially Ubuntu 15.04 – Andrew Kusachev Dec 09 '15 at 11:33
  • And what is your locale set to (see [this](http://stackoverflow.com/questions/12649896/why-doesnt-my-terminal-output-unicode-characters-properly) among many)? – trojanfoe Dec 09 '15 at 11:34
  • @LeśnyRumcajs Actually nothing changed in general. I wrote: `wchar_t * mesg = L"Просто строка ";` and he shows: `^_^D 9999` – Andrew Kusachev Dec 09 '15 at 11:54
  • Try using `std::wcout` instead of `wprintf`, if it fails you may have problems with your terminal and not the code itself. Check https://stackoverflow.com/questions/12649896/why-doesnt-my-terminal-output-unicode-characters-properly – Leśny Rumcajs Dec 09 '15 at 11:59
  • @LeśnyRumcajs @trojanfoe This is my locale file `LANG=ru_RU.UTF-8 LANGUAGE=ru LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=en_US.UTF-8` And if i type `$ echo -e "\xE2\x98\xA0"`, he shows me skull as he has. – Andrew Kusachev Dec 09 '15 at 12:23
  • @LeśnyRumcajs if i use `std::wcout << mesg << std::endl;` he shows me `?????? ???????` – Andrew Kusachev Dec 09 '15 at 12:26

1 Answers1

2

Without this ncurses library, but works well:

#include <iostream>

using namespace std;

int main() {
    setlocale(LC_ALL, "ru_RU.UTF-8");
    const wchar_t *mesg = L"Просто строка";
    for(int i = 0; i < 10000; i++)
    {
        std::wcout << mesg << i << std::endl;;
    }
    return 0;
}

The important parts: setLocale(), wchar_t and std::wcout.

Code in action: http://goo.gl/MtzMAO

Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33