V
V
Valery Gaisin2018-09-22 20:04:44
C++ / C#
Valery Gaisin, 2018-09-22 20:04:44

How to make the input of Russian text through the std:: cin operator normal and without kryakozyabr?

I decided to translate from English into Russian to translate the console game "Slovomeska" according to the book "Learning C ++ through game programming", the problem is in the words entered, which for some reason are converted into cracks (when I looked step by step), what is wrong with encodings and is there a simple solution
// Word Jumble
// The classic word jumble game where the player can ask for a hint
#include
#include
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
enum fields { WORD, HINT, NUM_FIELDS };
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "They surround you and protect you, at least she is one of those sisters who serve as supports for the house.."},
{"glass", "Through it you look, but it does not let the cold of the streets pass.." },
{"dog", "Barks, bites, won't let you in the house.."},
{"wishes", "The reason for all your suffering.."},
{"truth", "Unlike lies, it is soooo bitter ..."}
};
srand(static_cast(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word
string jumble = theWord; // jumbled version of word
int length = jumble.size();
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << "\t\t\tWelcome to the game \"Words\"\n\n";
cout << "Guess all the letters to form the word\n";
cout << "Type 'hint' for a little hint :) .\n";
cout << "Type 'quit' to leave us :( .\n\n";
cout << "Encrypted word: " << jumble;
string guess;
cout << "\n\n And your answer: ";
cin >> guess;
while ((guess != theWord) && (guess != "
}
else
{
cout << "Wrong guess.";
}
cout << "\n\nYour word: ";
cin >> guess;
}
if (guess == theWord)
{
cout << "\n Guessed? Well done! I'm proud of you (grandmother's voice)!\n";
}
cout << "\n Thanks for playing...\n";
system("pause");
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-09-22
@Dark7Lord

If you do it wisely, then you need to:
1.Save the sources in UTF8. For strings and string constants, use wchar_t or wstring.
2.When you start the program, find out the console encoding
3.Before displaying strings on the screen, convert your wide strings to the console encoding and only then output. For
Windows 2 Russian console encodings: cp1251 and cp866, with cp866 being used by default. You can change the console encoding from the console itself with the chcp command. Changing the console encoding from the program is a bad form.
An example of conversion using WinAPI can be found here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question