ascii heart in c++ windows 10 not displaying - c++

I'm working on a poker game in c++ using Visual Studio Express 2013 on Windows 10. When i use the following code to assign suits to my cards the console displays all question marks in the place of the suits.
void printHand(Card hand[])
{
const string SUIT = "\3\4\5\6";
const string RANK = "23456789TJQKA";
cout << "Your hand is: ";
for (int i = 0; i < SIZE; i++)
{
cout << RANK[hand[i].ranks] << SUIT[hand[i].suits] << " ";
}
cout << endl;
}
When I change the suits to other characters I get the right characters like question marks, colons... When I run a for loop to show all Ascii characters, the first 32 characters display as control characters like it doesn't recognize the font.
My question is whether this is because of Visual Studio 2013 Express, Windows 10, or my machine.

Check the checkbox in Properties that says "Use the old console" or something like that (Mine was in swedish). That solved the problem for me.

There are a number of things you must check:
Make sure your console is using Code Page 437
Failing that, make sure your program's locale is the default "C" locale.
Let us know if it still doesn't work.

Related

Unexpected console output for wstring literal and console input concatenation

Platform: Win 10 64 bit
IDE: CLION 2022.2.3
Toolchain: VS 2022 Community Toolset v17.0 (CMAKE 3.23.2)
Build Tool ninja.exe
C++ compiler: cl.exe
#include <iostream>
#include <string>
int main() {
std::wstring test1 = L"Hällo, ";
std::wstring test2;
std::cout << "Eingabe:" << std::endl;
std::wcin >> test2;
std::wcout << test1.append(test2);
}
User Input:
Hälp!
Console Output:
H├ñllo, Hälp!
I'm at the end of my wits here. I'm currently in the process of learning C++, and I'm dealing with some encoding problems. I have set both my file and console encoding to UTF-8. I am using wstring which should support umlauts. But for whatever reason I get a wrong output. I don't know where things go wrong.
My initial goal was to read in a txt file and count the number of characters per line and in total. I noticed, that I got a wrong count. For every ä, ö or ü my length() for the line would be off by 1 byte per Umlaut. So for
std::wchar text = L"äbc";
cout << text.length();
I would get:
4
Which struck me odd. Because I used wchar, and text.length() should have given me the number of wchar_t characters in my String, if I'm not mistaken.
When I try:
std::wchar text;
std::wcin >> text;
cout << text.length();
With the user input:
äbc
I would get the correct result of:
3
I am sincerely at the end of my wits. I don't know what causes those encoding errors. I would appreciate any help.
PS. I also tested the same code in Visual Studio, and I got the same unexpected behaviour.

Cout unsigned char

I'm using Visual Studio 2019: why does this command do nothing?
std::cout << unsigned char(133);
It literally gets skipped by my compiler (I verified it using step-by-step debug):
I expected a print of à.
Every output before the next command is ignored, but not the previous ones. (std::cout << "12" << unsigned char(133) << "34"; prints "12")
I've also tried to change it to these:
std::cout << unsigned char(133) << std::flush;
std::cout << (unsigned char)(133);
std::cout << char(-123);
but the result is the same.
I remember that it worked before, and some of my programs that use this command have misteriously stopped working... In a blank new project same result!
I thought that it my new custom keyboard layout could be the cause, but disabling it does not change so much.
On other online compilers it works properly, so may it be a bug of Visual Studio 2019?
The "sane" answer is: don't rely on extended-ASCII characters. Unicode is widespread enough to make this the preferred approach:
#include <iostream>
int main() {
std::cout << u8"\u00e0\n";
}
This will explicitly print the character à you requested; in fact, that's also how your browser understands it, which you can easily verify by putting into e.g. some unicode character search, which will result in LATIN SMALL LETTER A WITH GRAVE, with the code U+00E0 which you can spot in the code above.
In your example, there's no difference between using a signed or unsigned char; the byte value 133 gets written to the terminal, but the way it interprets it might differ from machine to machine, basing on how it's actually set up to interpret it. In fact, in a UTF-8 console, this is simply a wrong unicode sequence (u"\0x85" isn't a valid character) - if your OS was switched to UTF-8, that might be why you're seeing no output.
You can try to use static_cast
std::cout << static_cast<unsigned char>(133) << std::endl;
Or
std::cout << static_cast<char>(133) << std::endl;
Since in mine all of this is working, it's hard to pinpoint the problem, the common sense would point to some configuration issue.

Special Characters Not Working in Windows 10 [duplicate]

I'm working on a poker game in c++ using Visual Studio Express 2013 on Windows 10. When i use the following code to assign suits to my cards the console displays all question marks in the place of the suits.
void printHand(Card hand[])
{
const string SUIT = "\3\4\5\6";
const string RANK = "23456789TJQKA";
cout << "Your hand is: ";
for (int i = 0; i < SIZE; i++)
{
cout << RANK[hand[i].ranks] << SUIT[hand[i].suits] << " ";
}
cout << endl;
}
When I change the suits to other characters I get the right characters like question marks, colons... When I run a for loop to show all Ascii characters, the first 32 characters display as control characters like it doesn't recognize the font.
My question is whether this is because of Visual Studio 2013 Express, Windows 10, or my machine.
Check the checkbox in Properties that says "Use the old console" or something like that (Mine was in swedish). That solved the problem for me.
There are a number of things you must check:
Make sure your console is using Code Page 437
Failing that, make sure your program's locale is the default "C" locale.
Let us know if it still doesn't work.

How can i change colors in c++?

So i searched for it and i found many answers. I found that in c++ there's no standard cross-platform way to do that and that the operative system manages colors. For example i found that on windows you can use the system("color 1") statement to change color to the text ( or foreground) and the system("color A") to change color to the background, or both system("color 1A") to change both. But this will change the whole colors, and i was wondering if there was a way to change it like even for a single character. Like take the program that i just did as an example:
#include<iostream>
using namespace std; /* I prefer to use this because i think that's a huge time saver and it's also easier*/
void printRoad(int i) /* That's my function, so by this function it prints a number of times choosed by the user 4 pieces of road*/
{
int counter=1;
while (counter <= i)
{
system("color 2"); /*Here is what i was talking about. I used the system("color 2") statement to change the text color
from the default to green, but it changes the whole text.*/
cout << "** | **" << endl;
cout << "** | **" << endl;
cout << "** | **" << endl;
cout << "** | **" << endl;
counter++;
}
};
void main() /*I don't need any specific return value from either the main() and the function so i thought it was a good idea to
just use void.*/
{
cout << "How many piece of roads do you want to build?" << endl; /*Here it asks to the user what to do.*/
int pieces = 0;
cin >> pieces;
printRoad(pieces); //Here is the function call.
system("pause"); /* Because i'm using windows and i'm using Visual Studio Express 2013 I used system("pause") to pause
the program and let the user see the output.*/
}
So what if, for example, i'd like to change each piece of road color? Like the first cout<<"** | **"<
I also read many people complaining about the use of system("") statements. I understand it because by doing so your program lose the cross-platform ability. But if the thing is dependent on the system we're on, how should we do it by keeping the cross-platform ability? Thanks for any answer.
Actually you can use this instead of calling system():
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ValueOfColour);
As far as I understood your problem, you only want a certain character to be in your choosen colour. Then you need to change it back to the default value white/grey after this character was printed.

In C/C++, how do you edit a certain 'coordinate' in stdout?

I've been using Vim a lot lately, and I was wondering how the program manages to change the characters at certain positions in the terminal. For example, when using :rc, it replaces the character under the cursor with c.
I have also seen similar things done with Homebrew, which prints a progress bar to the screen and updates it when necessary.
How is this done in C/C++?
There is no standard way of doing this in C++.
It is done with OS dependent lbiraries, such as curses and similar libraries (ncurses) in the Unix/Linux world. Some of these libraries have been ported on across platforms (example: PDCurses)
For very simple things such as a progress bar or a counter, and as long as you remain on a single line there is the trick of using "\r" (carriage return) in the output, to place the cursor back at the begin of the current line. Example:
for (int i = 0; i < 100; i++) {
cout << "\rProgress: " << setw(3) << i;
this_thread::sleep_for(chrono::milliseconds(100));
}
Certainly, using ncurses or similar library is a good answer. An alternative may be to use ANSI Escape Codes to control the cursor in some terminal emulators (but not Windows command shell). For example, this code prints a line in multiple colors and then moves the cursor to 2,2 (coordinates are 1-based with 1,1 being the upper left corner) and prints the word "red" in the color red.
#include <iostream>
#include <string>
const std::string CSI{"\x1b["};
const std::string BLUE{CSI + "34m"};
const std::string RED{CSI + "31m"};
const std::string RESET{CSI + "0m"};
std::ostream &curpos(int row, int col)
{
return std::cout << CSI << row << ';' << col << 'H';
}
int main()
{
std::cout << "This is " << BLUE << "blue" << RESET << " and white.\n";
curpos(2,2);
std::cout << RED << "red" << RESET << '\n';
}
As mentioned that's not a matter of any C/C++ standard operations provided with stdout or cout (besides writing the necessary control characters to the screen).
Controlling the screen cursor of an ASCII terminal totally depends on implementation of the particular terminal program used, and besides a very narrow set of control characters, there's no standard established.
There are libraries like ncurses for a broader variety of linux terminal implementations, or PDcurses for a windows CMD shell.
I'm not sure to understand you completely but with creating an array of 100 elements of type char you can modify any position of the array and loop it with a std:cout to mostrate it on the console.
Perhaps could be better define the array of 50 chars to resuce the size of the printed result.
For example, if you have to print a progessbar in the 1% process, you should print:
Char progressbar[100] = {'X','','','','','','','','',........}