dev-c++ print out a heart symbol - c++

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.

Related

C++ and PowerShell: std::cout weird spacing with ANSI colors

I'm facing a weird issue with std::cout, ANSI color codes and PowerShell (integrated terminal in VS Code).
I have written a sample program that asks the user for string input. In the prompt I make use of ANSI color codes, so the default value (in case the user doesn't input anything) is displayed gray instead of white.
Running the executable works as intended the first time, but when running it a second time there's a weird spacing that disappears after the user presses Enter.
This issue persists until the code is compiled again.
I have noticed that when I leave out the ANSI color codes, this spacing also disappears. I'd still like to use colors though.
Please see below for sample code and images showing the output.
What causes this issue? Is there a different way of using colored std::cout output in C++?
Thanks a lot for your time.
Sample code
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Input: \x1b[37;2m(default value)\x1b[0m ";
std::getline(std::cin, input);
if(input.empty()) {
input = "default value";
}
std::cout << input << std::endl;
return 0;
}
Running the executable for the first time
Running the executable thereafter
Additional information
OS: Windows 10
Terminal: VS Code Integrated PowerShell
C++ compiler: g++ 8.1.0

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

How to correctly display playing card unicode characters in terminal?

I need to print the Unicode characters for the 52 playing cards to the terminal. But when I run the code I instead get an 'A' with odd accents for the suit, followed by the card number. Based on my own reading I think the limitation is the font the terminal is using. But I'm unsure how to fix that.
The terminal has no problem showing the suits themselves.
For example, the program has no issue with these: ♥♠♦♣
But is unable to correctly display these: 🂡,🃂,🃆 etc
This is what gets printed out:
This is on a Cent OS 7 VM.
#include <iostream>
int main() {
std::cout << "🂡\n"; // string literal
std::cout << "\xF0\x9F\x82\xA1\n"; // UTF-8 encoded octets
}
If you have the character in a string, you need to encode it yourself, see https://stackoverflow.com/questions/tagged/c%2b%2b%20utf-8.

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

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;
}