Home windows Russian text in C++ console applications

Russian text in C++ console applications [duplicate]

Author

Date

Category

For example, I write

# include & lt; iostream & gt;
using namespace std;
int main () {
  cout & lt; & lt; "I'm a program";
  return 0;
}

but it displays me not “I programmatically”, but some kind of №;%:? *:%; № "№;%:? * .


Answer 1

# include & lt; iostream & gt;
#include & lt; windows.h & gt;
char * rus (const char * text)
{
  char * buffRus = new char [strlen (text)];
  CharToOem (text, buffRus);
  return buffRus;
}
int main ()
{
  std :: cout & lt; & lt; rus ("Hello") & lt; & lt; std :: endl;
  return 0;
}

Answer 2

Use setlocale (LC_CTYPE, “Russian”);

# include & lt; iostream & gt;
using namespace std;
int main () {
setlocale (LC_CTYPE, "Russian");
cout & lt; & lt; "I'm a program";
return 0;
}

Answer 3

I suggest writing the program not with obsolete single-byte encodings, but in Unicode. Those. use wchar and derivatives. Use the _T () macro or the L literal to create a unicode literal.
Example:

L "this is a literal string"
_T ("Hello")

PS: there is wcout for unicode output:

std :: wcout & lt; & lt; L "TestString" & lt; & lt; std :: endl;

Answer 4

Then, when starting the program, right-click on the title bar (the top line of the window where the program name or window name is located) – & gt; Properties- & gt; Font- & gt; Lucida Console . And everything will work !!

# include & lt; iostream & gt;
using namespace std;
  int main () {
  SetConsoleCP (1251);
  SetConsoleOutputCP (1251);
    cout & lt; & lt; "I'm a program";
    return 0;

Answer 5

In the command line window, the encoding is NORMAL CP866. It can be changed to CP1251, which is standard for the rest of Windows with the chcp 1251 command, but it doesn’t do anything good. In general, two encodings in Windows are very inconvenient.

A good command line program should by default output text to a file (for example, when reassigning an output stream) in CP1251 encoding, and to a terminal in CP866. It is necessary to determine (for example, by calling isatty ()) where the output is directed and do the appropriate recoding from the string encoding in the program.


Answer 6


Answer 7

CharToOem worked.
SetConsole – NO 🙁

To Andrey Kuzmenko – before hitting your fingers – LEARN THE MATCH.
russian.h is not needed. * means a pointer to a type. I liked the code.
And don’t force me to tell you the difference between * , & amp; , and & amp; * .

char * func (int a, char *, float & amp; *)

– and so 🙂


Answer 8

// ConsoleApplication4.cpp: the main project file.
#include "stdafx.h"
#include & lt; conio.h & gt;
using namespace System;
int main ()
{
  Console :: WriteLine (L "Hello world!");
  _getch ();
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions