This question has already been answered:

I have to write Russian words in console letters in English letters. The program itself is Russian (I have Visual Studio 2010 express). Tell me what you need to do to be able to display Russian letters? Thank you in advance.

Found the answer (if anyone needs it)

#include<locale.h> int main() { setlocale(LC_ALL,"RUS"); 

Reported as a duplicate by participants PashaPash , Qwertiy , Cerbo , LEQADA , Alex 18 Oct '15 at 15:01 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 3
    <a href= hashcode.ru/questions/42373/.. target=_blank> Link </a> - chip
  • That's it. The same question is asked 100,000 times ... Somehow it is not good ... - 3JIoi_Hy6
  • setlocale (LC_ALL, "RUS"); (I checked in Windows 7, and in XP I don’t work for me) provides the correct output of Cyrillic from the program (constant) both to the console and to the file (redirected to stdout, cp1251 left !!!). Input is not recoded (you still need to do it with your hands). - avp

6 answers 6

Method 1: use wcout :

 #include <iostream> using namespace std; void main() { wcout.imbue(locale(".866")); wcout << L"Привет, Мир!"; } 

Alternatively, you can connect <windows.h> and use SetConsoleOutputCP(1251)
But for some reason this method did not work for me in 7ka, it was hard to figure it out. But it seems that for him it was necessary to switch some tick in the project settings, in general, use wcout , it is better, and it works not only in Windows.


Method 2:
I remembered another way, perhaps the easiest:

 setlocale(LC_ALL, "Russian"); cout << "Привет, Мир!" << endl; 

    Nothing needs to be installed. In identifiers, only Latin characters are allowed. And in unicode strings, you can write any characters, including Chinese characters.

    Example.

     #include <stdio.h> int main() { wprintf(_T("Привет, мир\n")); return 0; } 

    What is written in the lines: "Hello world", etc., should be placed inside the _T () macro for Unicode lines, and everything else should be Latin (except for comments).

    • Those. always write in Latin ?? - GALIAF95
    • Updated the answer. - stanislav
    • I didn’t help. I want to say hello world in Russian - GALIAF95

    Try this option.

     <windows.h> ostream & operator<<(ostream & out,char* s) { char buf[512],*p=buf; CharToOem(s,buf); while(*p) out<<*p++; return out; } 
    • 3
      Overloading the global operator << for ostream is evil. - Costantino Rupert
    • What could be the consequences? - OpenGLprogrammer
    • This is a real method for output in the correct encoding in Windows. CharToOem (from, to) rewrites the char [] buffer in cp-1251 encoding ( you write ANSI in it in the editor and it is in the Windows files ) into the char [] buffer converting the encoding into cp-866 (also known as DOS), which used in the console . The inverse transform is done by the OemToChar (from, to) function. These functions have synonyms that I personally find easier to remember (in the sense of what is being encoded into): AnsiToOem and OemToAnsi. But with the console there is one subtlety - the command line arguments typed in the console are passed to the program in cp1251 ! - avp
    • 3
      @OpenGLProgrammer Because global operators are global and apply to all objects of type std::ostream . Ie such code, for example, will pass all text going to a file via CharToOem : std::ofstream f("file.out"); f << "Text"; - Costantino Rupert

    When compiling console applications in the glorious C ++ programming language, you constantly encounter the problem of encodings. Russian letters are scribbled, and English is fine. The point is in the encodings. The Windows console uses OEM encoding. And in the Windows OS itself - ANSI encoding.

    To avoid problems with the output of Russian letters in the console, we will use the CharToOem function.

    The CharToOem function converts a string to an OEM character set. This function is the opposite of AnsiToOem. I think the question is closed :)

    • one
      > CharToOem ... opposite of the AnsiToOem function. Okay?) - Sh4dow
    • one
      AnsiToOem and CharToOem are synonyms #define AnsiToOem CharToOemA #define CharToOem CharToOemA is from winuser.h, usually connected from windows.h - avp
    • > Compiling console applications in the glorious C ++ programming language you constantly encounter the problem of encodings. You forgot to add "compiling and running in Windows. When I studied with ++, I switched to Linux (where I set up UTF-8, of course), and no problems with encodings (of course, some software still did its scribbles, but the console, editor, the compiler, which is, worked beautifully and harmoniously.) Therefore, anyone who asks me what is the best IDE for learning c / c ++ - I answer - Linux. - KoVadim
    • The real problem (not recognized by many) is that the constants in the program are in one encoding , and the data in the console or the files are in another . - avp
    • @KoVadim, a fortochnik and a dolphist in the face of me applaud the last phrase and completely agree) Sorry, could not resist) - Sh4dow

    Another option is to use _cwprintf, but it is only in Visual C ++.

      I don’t know if it works, but you can try, for example, if there is an option in the charms array in English in the ASCII table to find and replace with Russians, well, for example, (this is just an example), the English letter A has the number 20 and the Russian is 60. You you make a function, which, if you give an array, it will replace the 20 digit by 60, and the program will show the Russian a, but I don’t know how to do this with the cin operator.