5

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~^

Community
  • 1
  • 1
Saturn
  • 17,888
  • 49
  • 145
  • 271
  • This is an UTF8 issue. See this question: http://stackoverflow.com/questions/9922528/how-to-make-ncurses-display-utf-8-chars-correctly-in-c – ypnos Oct 15 '13 at 03:58
  • @ypnos: Thank you. However, it didn't seem to fix the problem, and now it prints this: `(M-b~X~^M-oM->~_M-c~CM-.M-oM->~_)M-b~X~^` - I've updated the question now. – Saturn Oct 15 '13 at 04:02
  • @Omega which locale did you set and just out of curiosity what's the encoding of your source-file? edit: forget that first part of my question, [this question](http://stackoverflow.com/questions/4703168/adding-unicode-utf8-chars-to-a-ncurses-display-in-c) cleared things up for me. – PeterT Oct 15 '13 at 04:09
  • @PeterT: For the locale, I just used the default like the answer the suggested: `setlocale(LC_CTYPE, "");`. As for the encoding, Ubuntu tells me that it is `ASCII English text`. – Saturn Oct 15 '13 at 04:14
  • @Omega that doesn't seem accurate, what does the editor you're using say regarding the file encoding? Also, did you try to use `-lncursesw` as suggested in the question I linked (probably not necessary, Ubuntu should have it as default). – PeterT Oct 15 '13 at 04:18
  • @PeterT: It says `Current Locale (UTF-8)`. Regarding `-lncursesw`, Ubuntu says `cannot find -lncursesw`. That's probably the problem then, I guess. Although I'm not sure why am I missing that. – Saturn Oct 15 '13 at 04:28
  • @Omega no, it's probably not the problem. Older linux distros used to have separate versions of ncurses, one with and one without wide-character support. Ubuntu probably only has the wide-character supporting one. – PeterT Oct 15 '13 at 04:36
  • @PeterT: Ah well. Is `UTF-8` right too? Damn. I think I'll try with another Linux distro, perhaps CentOS. – Saturn Oct 15 '13 at 04:44
  • @Omega turns out you were right. `apt-get install libncursesw5-dev` and `#include "ncursesw/ncurses.h"` actually make this work. Silly ubuntu devs. – PeterT Oct 15 '13 at 12:06

1 Answers1

8

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"

PeterT
  • 7,981
  • 1
  • 26
  • 34
  • 3
    Works great. Don't forget the `setlocale(LC_ALL, "");` thing before starting ncurses mode though. – Saturn Oct 16 '13 at 01:16
  • Remember to link with `ncursesw` as well, instead of the normal `ncurses` library: `g++ Main.cpp -o Main -lncursesw`. Just installing the `libncursesw5-dev` package will not be enough. – Per Lundberg Jun 26 '23 at 11:24