Displaying Up/Down Arrow C++ on Windows - c++

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

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 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.

Why does getchar work like a buffer instead of working as expected in real-time

This is my first question on stackoverflow. Pardon me if I haven't searched properly but I do not seem to find an explanation for this. Was just attempting an example from Bjourne Stroustroup's papers. Added my bits to see the array get re-sized as I type the text.
But it doesn't seem to work that way! getchar() simply waits till I am done with entering all the characters and then it will execute the loop. As per the logic, it doesn't actually go into the loop, get a character, perform its actions and then iterate. I am wondering if this is implementation specific, or intended to be like this?
I am on Ubuntu 14.04 LTS using Codeblocks with gcc 4.8.2. The source was in cpp files if that matters.
while(true)
{
int c = getchar();
if(c=='\n' || c==EOF)
{
text[i] = 0;
break;
}
text[i] = c;
if(i == maxsize-1)
{
maxsize = maxsize+maxsize;
text = (char*)realloc(text,maxsize);
if(text == 0) exit(1);
cout << "\n Increasing array size to " << maxsize << endl;
}
i++;
}
The output is as follows:
Array Size is now: 10
Please enter some text: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!
Increasing array size to 20
Increasing array size to 40
Increasing array size to 80
Increasing array size to 160
You have entered: this is some sample text. I would have liked to see the memory being realloced right here, but apparently that's not how it works!
Array Size is now: 160
This has nothing to do with getchar directly. The "problem" is the underlying terminal, which will buffer your Input. The Input is sent to the program after you press enter. In Linux (dunno if there is a way in Windows) you can workaround this by calling
/bin/stty raw
in terminal or by calling
system ("/bin/stty raw");
in your program. This will cause getchar to immediately return the input character to you.
Dont forget to reset the tty behaviour by calling
/bin/stty cooked
when done!
Here is an example (for Linux):
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
system ("/bin/stty raw");
char c = getchar();
system ("/bin/stty cooked");
return 0;
}
Also have a look at this SO Post: How to avoid press enter with any getchar()
Also, as suggested in the comments, have a look here: http://linux.die.net/man/3/termios especially on the command tcsetattr, which should work cross-platform.
Actually, tcsetattr does not apply to Windows (which is what is commonly referred to in this site as "cross-platform"). However, the question is tagged for Linux, so "cross-platform" is a moot point.
By default the standard input, output and error streams are set to
line-buffered (input)
block-buffered (output)
line-buffered (error)
You can change that using setbuf, but of course will not solve the problem (the answer calls for single-character input). POSIX terminal I/O (termios) lets you change via a system call any of the flags shown using stty. As a rule, you might call stty directly from a script, rarely from a C program.
Reading a single character is a frequently asked question, e.g.,
How can I read single characters from the terminal? (unix-faq)
How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed? (comp.lang.c FAQ)
You could also use ncurses: the filter function is useful for programs that process a command-line (rather than a full-screen application). There is a sample program in ncurses-examples (filter.c) which does this.

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.