Andrew Kusachev
Andrew Kusachev

Reputation: 123

C++ ncurses UTF-8 problems

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();

Upvotes: 2

Views: 898

Answers (1)

Leśny Rumcajs
Leśny Rumcajs

Reputation: 2526

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

Upvotes: 2

Related Questions