Home c++ Russian characters in C++ input and output

Russian characters in C++ input and output

Author

Date

Category

I started learning C++ and faced the problem of outputting Russian letters to the console. On our forum I learned that in order for C++ to display Russian letters correctly, you need to apply setlocale (LC_ALL, "Russian") and apply it, everything was fine until I read about entering strings and strings in the book and ran the example from the book … In the program whose code goes below, if you leave setlocale , then the program displays Russian letters normally, but if you request a string and enter Russian letters, and then display them, then they deteriorate on subsequent output. If you remove setlocale , then Russian letters do not deteriorate during input and then output, but their output from the program is corrupted.

// insrtl.cpp - read more than one line
#include & lt; iostream & gt;
int main () {
  using namespace std;
  setlocale (LC_ALL, "Russian");
  const int ArSize = 20;
  char name [ArSize];
  char dessert [ArSize];
  cout & lt; & lt; "Please enter your name: \ n";
  cin & gt; & gt; name;
  cout & lt; & lt; "Enter your favorite dessert: \ n";
  cin & gt; & gt; dessert;
  cout & lt; & lt; "I have a delicious" & lt; & lt; dessert;
  cout & lt; & lt; "for you," & lt; & lt; name & lt; & lt; ". \ n";
  system ("PAUSE");
  return 0;
}

Question: what should I do so that Russian letters can be output from the program and when prompted for input and subsequent output they do not get corrupted?

UPD : tried it like this:

// insrtl.cpp - read more than one line
#include & lt; iostream & gt;
#include & lt; windows.h & gt;
char * Rus (char * str)
{
  static char s [1024];
  CharToOem (str, s);
  return s;
}
int main () {
using namespace std;
  const int ArSize = 20;
  char name [ArSize];
  char dessert [ArSize];
  cout & lt; & lt; Rus ("Enter your name: \ n");
  cin & gt; & gt; name;
  cout & lt; & lt; Rus ("Enter your favorite dessert: \ n");
  cin & gt; & gt; dessert;
  cout & lt; & lt; Rus ("I have a delicious one") & lt; & lt; Rus (dessert);
  cout & lt; & lt; Rus ("for you,") & lt; & lt; Rus (name) & lt; & lt; ". \ n";
  system ("PAUSE");
  return 0;
}

But in general, it turns out that something strange looks like the output lines are written to variables: "I have a tasty" is written to desert and name is written "for you," . And what do I want Russian and not

C: \ Users \ Rules & gt; "D: \ ProjectsC++ \ C++ Learning \ Chapter 4 \ insrt1 \ insrt1.exe "
Enter your name:
Ivan
Enter your favorite dessert:
Cocoa
I have a delicious? & Nbsp; Є & nbsp; R for you,? Ў & nbsp; -.
Press any key to continue. ... ...

after my code and not what happened after the UPD, namely:

C: \ Users \ Rules & gt; "D: \ ProjectsC++ \ C++ Learning \ Chapter 4 \ insrt1 \ insrt1.exe "
Enter your name:
Ivan
Enter your favorite dessert:
Cocoa
I have a delicious one. I have a delicious one for you, for you,.
Press any key to continue. ... ...

Answer 1, authority 100%

SetConsoleCP (1251); // Console input encoded in 1251
SetConsoleOutputCP (1251); // Output to the console in 1251 encoding. You just need to change the console font to Lucida Console or Consolas

Answer 2

Used this function

# include & lt; windows.h & gt;
char * Rus (char * str)
{
  static char s [1024];
  CharToOem (str, s);
  return s;
}
...
std :: cout & lt; & lt; Rus ("Russian text");

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