How to print MACRON/Unicode in Windows Console - c++

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

Related

ANSI escape code not working properly [C++]

I am using the ANSI escape code to print colored output.
I am getting proper colored output in vs code integrated terminal.
But when I am running the program in external command-prompt/Powershell, I am not getting the expected colored output.
My program looks something like this:
#define RESET "\033[0m"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
int main(int argc, char** argv){
if(argc != 1){
std::cout << RED ">> Invalid Arguments!" RESET;
}else{
if(verify_password()){
...
...
...
}else{
std::cout << "\n>> " RED "Invalid Password!" RESET;
}
}
return 0;
}
Complete Code
NOTE: One weird thing I observed is that if I am entering the correct password then everything is working fine in both terminals(getting proper colors). But the problem is when either I am entering an incorrect password or an invalid amount of arguments
What might be the reason for this?
EDIT: I figured out a way to make it work. I find out that in order to make these escape codes work I need to have at least one call to system() function. But still, I am not sure how these things are connected.
Historically, consoles on Windows required use of the console API in the Windows SDK in order to do effects like color. But recent versions of Windows now support escape codes (and even UTF-8), but programs have to opt-in by calling SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING. (Opting in preserves backward compatibility for older programs that aren't using escape codes or UTF-8.)
If you're getting color in some places and not others, then I'd guess there's a problem related to the state the terminal/console is in when sending the escape codes. For example, if the terminal thinks it's already processing an escape code and a new one begins, it might not recognize either. I suppose this might also be a problem if one part of the program uses escape codes but another part uses the Console API.

dev-c++ print out a heart symbol

I'm trying to print out a heart symbol in dev-c++.
To print out a heart I wrote
#include <iostream>
using namespace std;
int main()
{
char heart = 3;
cout << "Heart = " << heart << endl;
return 0;
}
However, it doesn't print out a heart on my computer. I also changed the ANSI setting to "yes".
My classmates and professor can see the heart symbol but not me :/ Can anyone help with this?
While the ASCII code 3 is a control character, the default US Windows code page of 437 and Western European Windows code page of 850 will print Heart = ♥ in the cmd.exe window assuming the font selected supports it. This is backwards compatible with the old DOS code pages that printed symbols for some control characters. Your system may not be set to a font that supports it or it is a different code page. Use chcp to check the code page, chcp 437 to change it, and check the window's Properties page, Fonts tab. Consolas, Courier New, and Lucida Console fonts all support the heart.
C:\test>type test.cpp
#include <iostream>
using namespace std;
int main()
{
char heart = 3;
cout << "Heart = " << heart << endl;
return 0;
}
C:\test>cl /nologo /EHsc /W4 test.cpp
test.cpp
C:\test>chcp
Active code page: 437
C:\test>test
Heart = ♥
Screenshot (Consolas font):
A more reliable way on Windows to output the correct character is to set the console text mode to support wide characters and output the proper Unicode character. This still requires a font that supports the Unicode character, in this case U+2665 BLACK HEART SUIT, but won't care what code page the windows supports.
#include<fcntl.h>
#include <io.h>
#include <iostream>
int main () {
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L'\u2665' << std::endl;
}
The ascii character with the code 3 is a Control Character or non-printing character, specifically it's the End-of-Text character.
These characters have special meaning and do not represent a written symbol i.e. they are not meant to be printed.
Some terminals are configured (or can be configured) to print control characters (mostly for debugging purposes). Some of these are configured to print the EoT character as ^C, others as a heart, while others as something else like □ or �. Consult your terminal documentation to see if it can be made to print as you want.
But control characters should not be used to print symbols and should not be expected to print symbols.
If you want to print a heart symbol on the terminal you need to print an actual unicode heart symbol (e.g. U+2764 ❤) and the terminal must be configured for unicode and use a font that has that symbol.

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

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++?

Coloured output in Turbo C++

My compiler is Turbo C++ v3.0 with DOS v5.0 emulated in DOSBox v0.74
I use this because Turbo C++ is the compiler with which
my highschool has chosen to teach the C++ programming language.
It has been stressed that I use this compiler while coding my final term project.
I'm running Windows 8.1 (64 bit) with Intel Core i5-3317U CPU # 1.70GHz
For the sake of liveliness and in tribute to popular culture,
I want my output screens to have green text.
The following is what seemed to work :
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
textcolor(2); // text set to green colour (conio.h function)
cprintf("\n\t Hello World"); // cprintf from conio.h
cout << "\n\t Hello World"; // cout from iostream.h
getch();
}
The output of which is as follows (screen has been trimmed to save space on this post):
According to the Help Section in Turbo C++,
cprintf() sends formatted output to the text window on the screen.
As you can see, the text printed onto the screen by cout is not green and my project is composed of a lot of cin and cout and some writing and reading files.
The result I desire can (although I have not yet tried) most likely be obtained by replacing all my cout << "..."; with cprintf("...");
but I've written so many cout statements that it's going to be hard to edit the code that much.
cprintf is new territory for me and I'm slightly set aback by how
cprintf("\t"); is outputed as o
So, I'm not to keen using this. I don't wish to use this until and unless it is my only option.
The libraries cstdlib.h and windows.h are unavailable in Turbo C++ and hence I can't use their utilities to get what I want either.
In the end, all I want is that output prompt to display the text I've couted in bright green. Minimal change to my code would be nice.
I wouldn't even mind having to change some settings of my emulator or compiler or shell to do it.
All help is much appreciated. Thank you in advance =)
Ah, the 1990s called, they want their QEMM back :)
The one solution I can think of is to put this in your CONFIG.SYS:
DEVICE=C:\DOS\ANSI.SYS
and then output ANSI escape sequences.
You can use the constream library for colored text output:
#include <constrea.h>
int main()
{
constream cout;
cout << setclr(2);
cout << "\n\t Hello, World!" << endl;
getch();
return 0;
}
I don't know what to do about the tab character.
you just need to add the clrscr(); function after the textcolor(); and it works with the couts

Displaying Up/Down Arrow C++ on Windows

So unfortunately from what I have read, there isn't a hex code for an up and down arrow. There is only a "friendly code."(See bottom of linked page) Does anyone know how to use this friendly code? Ideally, I just want a printf() or std::cout statement that prints the up or down arrow, in C/C++.
Thanks! http://www.yellowpipe.com/yis/tools/ASCII-HTML-Characters/
Answers that assume a Unicode environment will not work with the Windows console at all, because it does not use Unicode. It uses a code page to determine which characters can be displayed and what the character codes are; on my US-based Windows 7 system that is Code page 437. You can see that the arrows are at the top of the list, but unfortunately they're in the control character range. This means they are stripped out of normal output entirely. You need special console output functions to display them.
Edit: It appears you don't need special console output, only a few characters like '\x0a' are stripped. The following string should print all 4 arrows: "\x18\x19\x1a\x1b".
Disregard this answer if you're using the Windows API and not a console program.
You need to use a std::wstring:
#include <iostream>
#include <string>
int main()
{
std::wstring s(L"←→↑↓");
std::wcout << s << "\n";
}
Note that there is a difference between L"←→↑↓" and "←→↑↓". The latter is invalid, as neither ← nor → and certainly not ↑ and ↓ are valid ASCII characters.
On my machine, this gets printed as <-->??, probably because the terminal doesn't support the characters.
Simply copy and paste the text of the arrow keys from the code below and add it into your code.
#include<iostream>
using namespace std;
int main(){
// for up, down arrows.
cout<<" ↑ ↓ ";
return 0;
}