How to print a non ascii-char in c++ [duplicate] - c++

This question already has answers here:
Output Unicode to console Using C++, in Windows
(5 answers)
Closed 3 years ago.
I'm learning C++ and I cannot figure out how how to print special characters in C++. Even when I've seen others post related to this issue, any of them solves this:
I just want to print out this chars -> '♦', '♥', '♣', '♠';
And when I do this ->
std::cout << '♦' << std::endl;
It prints out a number such as 14850470, but when I pass the char to a function, such as ->
char foo(char a)
{
return a;
}
int main()
{
std::cout << foo('♦') << std::endl;
}
It prints out 'ª' instead, any ideas?
(I'm writing this in VSCode with the MSVC compiler on Windows.)
EDIT:
The answers solved my problem (I executed chcp 65001 on the CL). But I have to change this std::cout << '♠' << std::endl; to this std::cout << "♠" << std::endl; in order to work, since printing as char prints nothing on the console.

There is no portable way.
On Linux/mac, the terminal recently adopted UTF-8 as default. So, when you output a UTF-8 binary to the standard output, you can see the '♦' character.
On Windows 10 1607 or later, chcp 65001 will work fine except when printing color emoji. Since the broken font config dialog is fixed and you can use a TrueType font for chcp 65001 console. In your program, you should output a UTF-8 binary to the standard output. Before you run your program, run chcp 65001 and configure the font.
On windows before Windows 10 1607, you should give up trying to print Unicode.
On C++20, the C++ standard committee adopt char8_t that's assumed to hold UTF-8. In the future, C++ (C++26? C++29?), when <locale> and <iostream> is recreated and support char8_t, you can printf a Unicode character portably.
In my opinion, you should give up trying to print Unicode characters. Create a GUI using some library which supports TrueType fonts and OpenType.

Assuming you're happy to have your code only work in Windows I think the answer to your question can be found here:
How to use unicode characters in Windows command line?
The answer doesn't go into a lot of detail, this should help: https://learn.microsoft.com/en-us/windows/console/low-level-console-output-functions
This way might be quicker and easier though: How to print Unicode character in C++?

Related

output stream in C++ [duplicate]

This question already has answers here:
"std::endl" vs "\n"
(10 answers)
Closed 2 years ago.
If I have opened an output stream like this:
ofstream to(output_file);
How may I print new line (Looking to support different os)?
to << "\n";
to << "" <<endl;
End of line notation, '\n' is used in most of the systems I know except Windows (MS Dos) which use '\r\n'. What is different in between those two is that '\n' in most systems will return cursor to the beggining of new line, where in MS DOS it will go to the same character as previous line, so if you have something like this:
Hello\nWorld!!!
in most systems it will output:
Hello
World!!!
where as in MS DOS it will output:
Hello
World!!!
So to overcome this issue of different systems treating newline differently we have std::endl, which will place correct notation for the correct system. In my code I might be bad, but I am mostly using '\n', but on the other hand I have not been using Windows as my dev machine that much.
One more point, printing out "" is useless.

Why does my c++ program output garbled code

I use MinGW64 to compile c++ programs. But since I upgraded to Windows 10, I found my c program output Chinese will be garbled code.
I follow the online method, adding a code in the program header: SetConsoleOutputCP(65001);, then it fixes. but I think it's so troublesome to do this for each c++ program. What should I do?
I think this is my system's problem, the same code in Windows 7 is not a problem, I just want to find a more convenient solution instead of adding the same code to every file
There's the code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SetConsoleOutputCP(65001);
cout << "文本" ; //Output will be garbled code if there's no line above
return 0;
}
The console in most operating systems only expects ASCII character input. In order to show some other char set you have to specify that in your code. The SetConsoleOutputCP command sets the "code page" windows should read from. By the way not all versions of windows have the same code for this command.
Please refer to the documentation found here.
The documentation suggests using EnumSystemCodePages to make sure the code for that language exists on that system.
P.S.
Your English is very good :)
EDIT
I tested your code on my computer with Visual Studio 2019 and got the following
Warning C4566 character represented by universal-character-name '\u6587' cannot be represented in the current code page (1255)
even with the SetConsoleOutputCP command you added. I assume you need to have chines installed for this to work. The problem is that I don't have the relevant code page for the windows console to look in for the char set. see this answer and this answer.

How to print MACRON/Unicode in Windows Console

So currently I'm working with Unicode a lot lately.
I've seen others facing the same issue but so far no answer solved my issue.
The objective is at the moment is to be able to print: ¯ in my Windows Console Window.
The character is called "MACRON" and its Unicode number is U+00AF.
At first I simple wrote: cout << "some irrelevant text lorem ipsum etc... \u00AF" << endl;
But this ended up backfiring at me with the windows console window displaying a weird indescribable ugly (no offence) looking T.
I've also tried to use wcout wcout << L"some more irrelevant text lorem ipsum etc... \u00AF" << endl;
but the outcome is the same.
Any thoughts as to why my source code/console window is unable to print the MACRON character?
A fix for this character is good but overall I'll encounter weirder and weirder Unicode character so I may need a broader applicable solution without downloading/changing anything outside of the source code.
Programming with C++ in Code::Blocks 17.12 IDE
Use _setmode(..., _O_U16TEXT);
#include <io.h>
#include <fcntl.h>
...
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"\u00AF\n";
Just make sure to read the caveats on the docs page to make sure you're happy with the trade-offs of using it (for example plain printf will no longer work in that mode):
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2017

swprintf fails with unicode characters in xcode, but works in visual studio

While trying to convert some existing code to support unicode characters this problem popped up. If i try to pass a unicode character (in this case im using the euro symbol) into any of the *wprintf functions it will fail, but seemingly only in xcode. The same code works fine in visual studio and I was even able to get a friend to test it successfully with gcc on linux. Here is the offending code:
wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");
wprintf(L"return: %d\n", _iRetVal);
// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
wprintf(L"%d: (%d)\n", i, _teststring[i]);
}
In xcode the call to swprintf will return -1, while in visual studio it will succeed and proceed to print out the correct values for each of the 3 chars (65, 165, 8364).
I have googled long and hard for solutions, one suggestion that has appeared a number of times is using a call such as:
setlocale(LC_CTYPE, "UTF-8");
I have tried various combinations of arguments with this function with no success, upon further investigation it appears to be returning null if i try to set the locale to any value other than the default "C".
I'm at a loss as to what else i can try to solve this problem, and the fact it works in other compilers/platforms just makes it all the more frustrating. Any help would be much appreciated!
EDIT:
Just thought i would add that when the swprintf call fails it sets an error code (92) which is defined as:
#define EILSEQ 92 /* Illegal byte sequence */
It should work if you fetch the locale from the environment:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(void) {
setlocale(LC_ALL, "");
wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");
wprintf(L"return: %d\n", _iRetVal);
// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
wprintf(L"%d: (%d)\n", i, _teststring[i]);
}
}
On my OS X 10.6, this works as expected with GCC 4.2.1, but when compiled with CLang 1.6, it places the UTF-8 bytes in the result string.
I could also compile this with Xcode (using the standard C++ console application template), but because graphical applications on OS X don't have the required locale environment variables, it doesn't work in Xcode's console. On the other hand, it always works in the Terminal application.
You could also set the locale to en_US.UTF-8 (setlocale(LC_ALL, "en_US.UTF-8")), but that is non-portable. Depending on your goal there may be better alternatives to wsprintf.
If you are using Xcode 4+ make sure you have set an appropriate encoding for your files that contain your strings. You can find the encoding settings on a right pane under "Text Settings" group.
Microsoft had a plan to be compatible with other compilers starting from VS 2015 but finally it never happened because of problems with legacy code, see link.
Fortunately you can still enable ISO C (C99) standard in VS 2015 by adding _CRT_STDIO_ISO_WIDE_SPECIFIERS preprocessor macro. It is recommended while writing portable code.
I found that using "%S" (upper case) in the formatting string works.
"%s" is for 8-bit characters, and "%S" is for 16-bit or 32-bit characters.
See: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
I'm using Qt Creator 4.11, which uses Clang 10.

Superscript in C++ console output

I'd like to have my program output "cm2" (cm squared).
How do make a superscript 2?
As Zan said, it depends what character encoding your standard output supports. If it supports Unicode , you can use the encoding for ²(U+00B2). If it supports the same Unicode encoding for source files and standard output, you can just embed it in the file. For example, my GNU/Linux system uses UTF-8 for both, so this works fine:
#include <iostream>
int main()
{
std::cout << "cm²" << std::endl;
}
This is not something C++ can do on its own.
You would need to use a specific feature of your console system.
I am not aware of any consoles or terminals that implement super-script. I might be wrong though.
I was trying to accomplish this task for the purpose of making a quadratic equation solver. Writing ax² inside a cout << by holding ALT while typing 253 displayed properly in the source code only, BUT NOT in the console. When running the program, it appeared as a light colored rectangle instead of a superscript 2.
A simple solution to this seems to be casting the integer 253 as a char, like this... (char)253.
Because our professor discourages us from using 'magic numbers', I declared it as a constant variable... const int superScriptTwo = 253; //ascii value of super script two.
Then, where I wanted the superscript 2 to appear in the console, I cast my variable as a char like this...
cout << "f(x) = ax" << (char)superScriptTwo << " + bx + c"; and it displayed perfectly.
Perhaps it's even easier just to create it as a char to begin with, and not worry about casting it. This code will also print a super script 2 to the console when compiled and run in VS2013 on my Lenovo running Windows 7...
char ssTwo = 253;
cout << ssTwo << endl;
I hope someone will find this useful. This is my first post, ever, so I do apologize in advance if I accidentally violated any Stack Overflow protocols for answering a question posted 5+ years ago. Any such occurrence was not intentional.
Yes, I agree with Zan.
Basic C++ does not have any inbuilt functionality to print superscripts or subscripts. You need to use any additional UI library.
std::cout << cm\x00B2;
writes cm^2.
For super scripting or sub scripting you need to use ascii value of the letter or number.
Eg: Super scripting 2 for x² we need to get the ascii value of super script of 2 (search in google for that) ie - 253. For typing ascii character you have to do alt + 253 here, you can write a any number, but its 253 in this case.
Eg:-cout<<"x²";
So, now it should display x² on the black screen.
Why don't you try ASCII?
Declare a character and give it an ASCII value of 253 and then print the character.
So your code should go like this;
char ch = 253;
cout<<"cm"<<ch;
This will definitely print cm2.