0

I'm trying to output directional arrows for a simple snake game in C++ on Windows 10. However, using this table as reference:

ASCII reference

All I got is this tiny question mark in the console:

Tiny question mark

I would like to output the symbols 16, 17, 30 and 31. I'm not much of programmer so it could be some basic mistake, but some symbols do work while others result in that symbol above.

A small example:

void showSnake() {
    char snakeHead;
    snakeHead = 31;
    cout << snakeHead; //THIS SHOWS THE TINY QUESTION MARK
    snakeHead = 62;
    cout << snakeHead; //THIS SHOWS THE ">" SYMBOL
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    Use Unicode characters if you can. That's not ASCII despite general assumptions to the contrary, that's [DOS ANSI](https://en.wikipedia.org/wiki/Code_page_437). – tadman Nov 22 '19 at 18:35
  • 1
    I don’t know where you got that reference, but it’s _not_ [ASCII](https://en.wikipedia.org/wiki/ASCII#Character_set). – Biffen Nov 22 '19 at 18:36
  • Press Alt+Space > Properties > Font tab. You need to select one of the olden raster fonts. – Hans Passant Nov 22 '19 at 18:45
  • That's a variant of ASCII known as Not ASCII (or OEM 437). – Eljay Nov 22 '19 at 18:47
  • genpfault - Just edited the question with an example of my attempt. tadman - My bad, I didn't know that Biffen - I got it after type "ascii table c++" on google images... Maxime.D - Already tried this, thank you though. HansPassant - Not sure if I got it, is this would solve the issue? I'll try anyway.. Eljay Yeah - other guys mentioned that it isn't ASCII... – Gabriel Minosso Nov 22 '19 at 18:57
  • 1
    There are no arrows in ASCII. The "reference" you are looking at is a lie. – n. m. could be an AI Nov 22 '19 at 19:14
  • In *standard* ASCII, anything less than 32 (0x20) is a *control character* and may not have a printable glyph. – Thomas Matthews Nov 22 '19 at 19:56

1 Answers1

1

You should use Unicode, you'll have much more choices for characters.

On https://en.wikipedia.org/wiki/List_of_Unicode_characters I found this symbol '▶' which looks similar to what you wanted to use.

Its unicode value is U+25BA which means you can create a character with a value of '\u25BA' in C++.

In practice however that value would go outside the range of the char type so you have to use wide characters (wchar) to get the job done.

As per this answer you should also toggle support for Unicode character in stdout using the _setmode function (see here) from the C run-time library.

#include <iostream>
#include <io.h>
#include <fcntl.h>

int main() {
     _setmode(_fileno(stdout), _O_U16TEXT);
     std::wcout << L'\u25BA';
}
SpectreVert
  • 187
  • 2
  • 10
  • 1
    "You should use Unicode". Let's make Microsoft use Unicode first, shall we? Or C++. Dealing with Unicode in C++ is a task for experts. – n. m. could be an AI Nov 22 '19 at 19:17
  • 1
    @n.'pronouns'm. That depends on how far one wants to take it. In this case, not very far at all. – Deduplicator Nov 22 '19 at 19:31
  • Thanks, but I tried in my visual studio and nothing is showed. I notice that you mistakenly put the "u" in front of "\", even changing it to the right order, nothing is presented in my console. Tried with and without the "setlocale" line. Any idea? – Gabriel Minosso Nov 22 '19 at 19:31
  • For Windows with VC++, if the stdout is a console file (e.g. `GetConsoleMode(_get_osfhandle(_fileno(stdout)), &mode)` succeeds), then we should switch the low I/O C file-descriptor mode to UTF-16 via `_setmode(_fileno(stdout), _O_U16TEXT)`. This avoids mojibake from encoding to the console's output codepage, which is typically OEM (e.g. US codepage 437). – Eryk Sun Nov 22 '19 at 19:36
  • With Windows 8 and above, changing the console output codepage to UTF-8 (65001) and using UTF-8 byte strings instead of wide-character strings is also possible via `SetConsoleOutputCP(CP_UTF8)`, but it's a complete mess for older versions of Windows. Setting the input codepage to UTF-8 via `SetConsoleCP` is still broken (limited to 7-bit ASCII) in all versions, including Windows 10. – Eryk Sun Nov 22 '19 at 19:36
  • @GabrielMinosso No, you are mistaken. Please read [this](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/). – n. m. could be an AI Nov 22 '19 at 19:42
  • @Deduplicator No further than printing a string. I didn't invent this, a member of the C++ standard committee has told me. – n. m. could be an AI Nov 22 '19 at 19:45
  • @ErykSun Sorry, I didn't understand very well what you meant... – Gabriel Minosso Nov 22 '19 at 19:54
  • @n.'pronouns'm. How so? does the code was right? I tried with the "u" in front of the "\" too and all I got was an "u" character in the console... It's just simple arrows , I'm getting angry about this language, most of the things about it seems complicated... – Gabriel Minosso Nov 22 '19 at 19:54
  • @GabrielMinosso, if you're a novice, there's too much to explain, sorry. It's off topic, but I recommend Python for learning. Python 3.6+ has good support for Unicode in the Windows console and defaults to UTF-8 as the source file encoding (so your source file can contain `'►'` instead of `'\u25ba'`). You can get down to the business of learning to develop a program, including learning about Unicode, without getting overwhelmed by technical details and jargon. Later on you can and should come back to learn C and C++. – Eryk Sun Nov 22 '19 at 20:16
  • @ErykSun I'm kinda a novice, yes. Already have a prototype or two in Unity, which uses C#. This project is for a college work that must be finished till Tuesday, I'm afraid I can't change the language now. If it's that hard just to insert arrows, maybe I'll choose another kind of representation and try to adapt what have so far... Hopefully I'll not have to work with this language again, it seems unecessarily complicated, at least for simple games like this. Thanks anyway. – Gabriel Minosso Nov 22 '19 at 22:09
  • @GabrielMinosso Sorry, u in the front of \ is incorrect, I just misread it. – n. m. could be an AI Nov 23 '19 at 06:47
  • @n.'pronouns'm. Ok, don't worry. Even type it right, I don't get anything when I run the project. Check it out: https://i.imgur.com/T8yBrBr.png Do you know what's is missing this time? – Gabriel Minosso Nov 23 '19 at 19:53
  • @GabrielMinosso take a look at that answer https://stackoverflow.com/a/9051543/11269176 – SpectreVert Nov 23 '19 at 20:01