Есть игра "Виселица", которая написана для работы только с английскими символами. Хочу добавить русские слова. Не получается заменить тип char в программе на другие, чтобы была возможность ввода и обработки символов кириллицы.
char guess;
Поискав похожие темы, понял что, символы Unicode кодируются двумя байтами, а тип char может помещать один. Поэтому в игре вместо кириллицы отображаются ромбы со знаками вопросов.
Подскажите, как правильно заменить char на wchar_t, чтобы компилятор не выдавал ошибку в строках сравнения THE_WORD[i] == guess и soFar[i] = guess;
// update soFar to include newly guessed letter
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
Система Ubuntu 14.04, компилятор GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit Qt Creator 3.6.0 Based on Qt 5.5.1
Вот весь код
// Hangman
// The classic game of hangman
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
int main()
{
// set-up
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector<string> words; // collection of possible words to guess
words.push_back("монитор");
words.push_back("телескоп");
words.push_back("смартфон");
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_WORD.size(), '*'); // word guessed so far
string used = ""; // letters already guessed
cout << "Welcome to Hangman. Good luck!\n";
// main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.\n";
// update soFar to include newly guessed letter
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}
// shut down
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
wchar_t
— правильный путь. Вот вам небольшой гайд по русификации консольных программ: ru.stackoverflow.com/a/459299/10105 Кроме того, замените вездеchar
наwchar_t
,string
наwstring
, константы типа"abc"
на константы типаL"абв"
,cin
/cout
/cerr
наwcin
/wcout
/wcerr
. – VladD 16 янв '16 в 19:07