Good afternoon, the problem with the encodings.

string name = "увыц"; cout << name; 

Displays krakazyably, tried this:

  SetConsoleCP(1251); SetConsoleOutputCP(1251); string name = "увыц"; cout << name; 

And a couple of ways from the forums did not help.

  • which kryakazyably? Maybe your editor in utf-8 saves the text. - KoVadim
  • General question marks, if you take this option - fortunado
  • 2
    Throw Windu - and you will be happy. - avp
  • Yes, I would quit, but on the instructions you need to write a project for it - generous) - fortunado

2 answers 2

if you have windows:

 #include <iostream> #include <locale> #include <string> #include <Windows.h> using namespace std; int main(){ setlocale(LC_ALL, "rus"); SetConsoleCP(866); string s = "абвгде"; cout << s << endl; return 0; } 

 абвгде Для продолжения нажмите любую клавишу . . . 
  • displays question marks, compiled in vs2010 - fortunado
  • one
    You probably have problems with system fonts, maybe the font is completely missing or the font is pure English - perfect
  • if so, then it’s exactly what I’m introducing, and Russian too. SetConsoleCP (1251); SetConsoleOutputCP (1251); string name; cin >> name; cout << name; - fortunado
  • one
    My advice is to never use string , just wstring . Otherwise, problems with the coding of your editor, your system, the system code page, and the like will be added to problems with the encoding of the console. - VladD
  • one
    @fortunado: on SO it is advised: #include <string> #include <iostream> #include <io.h> #include <fcntl.h> using namespace std; int wmain (int argc, wchar_t * argv []) {_setmode (_fileno (stdout), _O_U16TEXT); // witchcraft here wstring s = L "begin byws end"; wcout << s << endl; return 0; } - VladD

so it worked, there really were problems with the encoding in the system, if you explicitly specify, then everything works with wstring, it doesn’t want with string.

Working option:

  std::locale::global(std::locale("")); system("chcp 65001"); wstring name = L"увыц"; wcout << name; 

ATP for attention.

  • one
    @fortunado, are you sure that you want to communicate with a poor Windows Unicode? Or is it still in the program cp1251 (the usual encoding of files, etc. in Windows), and on the console the standard cp866? You search the HashCode for similar questions. There are simple working examples. Search via Google: site: hashcode.ru the words of your request an example of the answer - avp