Search code examples
c++ubuntuterminalncursescurses

How can I print (☞゚ヮ゚)☞ with Ncurses?


I would like to print (☞゚ヮ゚)☞ with the Ncurses library using C++ in Ubuntu.

First of all, you can do this by simply having:

std::cout << "(☞゚ヮ゚)☞" << std::endl;

And it works just fine.

However, when printing using Ncurses, I think that you need to use printw(char[]). In which case, I try something like this:

std::string str = "(☞゚ヮ゚)☞";   // String
initscr();                     // Start curses mode
printw(str.c_str());           // Print
getch();                       // Wait for input
endwin();                      // Exit curses mode

But it outputs:

(�~X~^��~�~C���~)�~X~^

I had thought that maybe it was c_str()'s fault, but when I do it with std::cout it works just fine too.

How can I print that text with Ncurses? Why does it work with std::cout and not with Ncurses' printw(char[])?

I compile using

g++ Main.cpp -lncurses

In a 64-bit machine. Ubuntu (64 bits too) is running in VirtualBox with OSX as host.


Update:

I've been redirected to https://stackoverflow.com/a/9927113/555690. The solution there doesn't seem to fix my problem - instead, this is how it looks now:

(M-b~X~^M-oM->~M-c~CM-.M-oM->~)M-b~X~^


Solution

  • I guess I'll post this as the answer. So, Ubuntu does apparently not by default ship with the Unicode supporting version. So you first need to install it with

    sudo apt-get install libncursesw5-dev
    

    then you can compile this

    #include <iostream>
    #include <string>
    #include "locale.h"
    #include "ncursesw/ncurses.h"
    using namespace std;
    
    int main()
    {
        setlocale(LC_ALL, "");
        std::string str = "(☞゚ヮ゚)☞";   // String
        initscr();                     // Start curses mode
        printw(str.c_str());           // Print
        getch();                       // Wait for input
        endwin();  
        return 0;
    }
    

    and it'll work without a hitch.

    Mind the #include "ncursesw/ncurses.h"