Special Characters Not Working in Windows 10 [duplicate] - 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.

ascii heart in c++ windows 10 not displaying

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.

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','','','','','','','','',........}

Win32 - Processing the backspace command?

TL;DR I need to know which characters are deleted with Win32.
Basically, I need to know what characters are deleted, each time they are. Considering that there are three methods to delete, (Backspace, delete, and Right click>Delete) I'm not sure if I'll just be re-using the same code or what. There's also the option of multiple characters being selected, as well as the option for undo/redo. There's probably something else I'm missing.
As I said above, I need to know which characters are deleted, and how to use undo/redo and how to tell if when using those if characters were added or deleted. (If that's something easily Google, tell me, I just thought of them as I've been writting this post)
What exactly are you talking about? The win32 default control windows? You can subclass them to do that...
http://msdn.microsoft.com/en-us/library/bb773183%28v=vs.85%29.aspx
Well, if you come up with something specific code-wise and don't quite know what to do, just ask here and see if someone can't help you.
With regard to undo/redo, I believe it all depends on the size of the focus area, and I'm not sure how MS does it for, say, Word, or Excel, etc.
But for short stuff, what I posted above should work in a universal sense, but apparently not in your case.
Nevertheless, if anyone wanted to shorten the iteration in the above, they could replace the loop above by starting right at the end of shorter string, like this --
for(int i = iLen2, x=0; i < iLen1; i++, x++)
szAnswer[x]=str1[i];
This simply confines the interation to the missing characters.
Here is a small Win32 Console application that will demonstrate a very simple way to determine which characters have been deleted. You can easily adapt this to a Win32 App, add iterations to it, and so on
#include <iostream>
#include <string>
using namespace std;
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
int main()
{
int iLen1, iLen2, iResult;
char str1[]="The old red box.";
char str2[]="The old red";
char szAnswer[MAX_PATH]="";
// strcmp() will be greather than 0
// if str1 is longer than str2
if(strcmp(str1, str2) > 0)
{
iLen1=strlen(str1);
iLen2=strlen(str2);
iResult=iLen1-iLen2;
cout << "The number of missing characters is " << iResult << endl << endl;
// now lets see which characters are missing
// we iterate through the entire length
// of str1, which is the full text, but
// we only start puting characters into our
// result when we get to the end of str2
for(int i=0, x=0; i < iLen1; i++)
{
if(i >= iLen2)
{
szAnswer[x++]=str1[i];
}
}
cout << endl << "The missing characters are --" << endl << endl << szAnswer << endl << endl;
}
return 0;
}