Code:

#include <iostream>; #include <cstdio>; using namespace std; int main() { setlocale(LC_ALL,"Russian"); int i; char numbers[][81] = { "Π‘Π°Π½Ρ‘ΠΊ", "24567", "Π‘Ρ€Π°Ρ‚", "23145", "Мама", "25674", "Папа", "26475", "Π›ΡŽΠ±ΠΈΠΌΠ°Ρ", "24534"}, name[81]; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя: "; cin >> name; for (i=0; i<=10; i+=2) { if (!strcmp(name,numbers[i])) { cout << "Π’Π΅Π»Π΅Ρ„ΠΎΠ½ " << name << ": " << numbers[i+1] << "\n"; break; } if (i == 10) { cout << "НичСго Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ\n"; } } system("pause"); return 0; } 

Result: http://smotr.im/assa

Why it happens?

PS I just started learning C ++.

  • Judging by the algorithm and programming too long? - Niki-Timofe
  • This is an example from a textbook. - MrILLUMINOR
  • 3
    Consider this line for (i = 10; i <= 10; i + = 2) - skegg
  • 3
    I recommend that you, as a beginner, forget about these problems and use only the Latin in programs. Upon reaching a certain level of knowledge, this issue can be returned. - skegg
  • one
    In ubuntu it can be, but probably in the TS Windows, and there the "Russian" is 1251, and in the console 866, hence the problems. - Mirdin

2 answers 2

I will look at the people here who do not want to learn at all, and wchar_t lives with phobias, wondering in the coffee grounds.

I repeat once again - it is necessary to clearly distinguish the encoding of your characters and strings:

  1. Character and string literals will have the same encoding as the file.
  2. External data sources (DB, files, network, etc.) - you need to specify the appropriate encoding and / or convert manually if necessary.
  3. Strings at runtime - when working with third-party libraries that use different encoding or who do not know how to use it at all.

By sabzh: analyze an example easier to find out where what

 //цСлСвая систСма - windows, ΠΈΠ±ΠΎ Ρƒ Π’Π‘ вСндовая консоль Π½Π° ΡΠΊΡ€ΠΈΠ½ΡˆΠΎΡ‚Π΅ #include <iostream> using namespace std; int main() { //строка Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ CP1251, Π΄Π΅Ρ„ΠΎΠ»Ρ‚Π½ΠΎΠΉ для тСкстовых Ρ„Π°ΠΉΠ»ΠΎΠ² Π² Π²Π΅Π½Π΄Π΅ const char* iName = "Π‘Π°Π½Ρ‘ΠΊ"; char oName[6]; //Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ сиволы Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ Π‘P1251 cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя: "; //ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ крякозяблы, ΠΈΠ±ΠΎ консоль Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ DOS 866 //Π²Π²ΠΎΠ΄ΠΈΠΌ с консоли Π² Ρ‚ΠΎΠΉ-ΠΆΠ΅ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ cin >> oName; //сравниваСм строки ΠΏΠΎΠ±Π°ΠΉΡ‚Π½ΠΎ, ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Ρ„ΠΈΠ³Π½ΡŽ cout <<strcmp(iName, oName)<<endl; return 0; } 

what we can / should do

  #include <iostream> #include <windows.h> using namespace std; int main() { //строка Π² ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅ CP1251, Π΄Π΅Ρ„ΠΎΠ»Ρ‚Π½ΠΎΠΉ для тСкстовых Ρ„Π°ΠΉΠ»ΠΎΠ² Π² Π²Π΅Π½Π΄Π΅ const char* iName = "Π‘Π°Π½Ρ‘ΠΊ"; char oName[6]; //Ρ‡Ρ‚ΠΎΠ±Ρ‹ вывСсти Π½ΠΎΡ€ΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ тСкст //1. Π»ΠΈΠ±ΠΎ устанавливаСм локаль для Ρ€Π°Π½Ρ‚Π°ΠΉΠΌΠ° //setlocale(LC_ALL, "russian_russia.1251"); //1 //2. Π»ΠΈΠ±ΠΎ устанавливаСм Π½ΡƒΠΆΠ½ΡƒΡŽ ΠΊΠΎΠ΄ΠΎΠ²ΡƒΡŽ страницу для консоли //SetConsoleOutputCP(1251); //2 cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя: "; //3. Ρ‡Ρ‚ΠΎΠ±Ρ‹ ввСсти Π² ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎΠΉ ΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²ΠΊΠ΅, ставим ΠΊΠΎΠ΄ΠΎΠ²ΡƒΡŽ страницу для Π²Π²ΠΎΠ΄Π° //SetConsoleCP(1251); //3 cin >> oName; //сравниваСм строки ΠΏΠΎΠ±Π°ΠΉΡ‚Π½ΠΎ cout <<strcmp(iName, oName)<<endl; return 0; } 

Ps. for learning from scratch C ++ as a first language Lippmann recommended textbook.

Pps. We smoke 14 chapter of Josattis and here is this fact

  • one
    In this case, do not forget to switch the font in the console window on the "Lucida Console". But I don’t know how to do it programmatically (and if at all). PS On your computer, this operation can be done once and remember the font for subsequent calls to the console. - If you do for console programs as you recommend, then you are tortured to tell users how to work with the program. Ugly, but reliably, stupidly recode all console I / O between cp866 and cp1251 (CharToOem (source_cp1251_str, output_cp866_str) and its reverse OemToChar (input_cp866_str, working_cp1251_str)). - avp
  • I wonder how the title <windows.h> will work in Linux, or in some other system / compiler? - Mirdin
  • @Mirdin, you can give advice about \ #ifdef, etc., but in reality, a cross-platform and modifiable (easy to maintain) application with the Russian language is a utopia (unfortunately). - avp
  • @avp but at the same time, what is the point of writing an application on c ++ that is executed only in one environment, and is compiled only by one compiler, is it not better then to allocate a piece of the project that requires C ++ to a separate branch, and the interface, including the console , much easier to write on Sharpe, Java or some other language. - Mirdin
  • one
    > Unfortunately, site search works not so good. Googlo site search: < google.com/search?q=site:hashcode.ru+setlocale+windows > - mega

Try this

 #include <iostream> #include <locale> #include <cstdio>; using namespace std; int main() { setlocale(LC_ALL,".866"); int i; wchar_t numbers[][81] = { L"Π‘Π°Π½Ρ‘ΠΊ", L"24567", L"Π‘Ρ€Π°Ρ‚", L"23145", L"Мама", L"25674", L"Папа", L"26475", L"Π›ΡŽΠ±ΠΈΠΌΠ°Ρ", L"24534"}, name[81]; wcout << L"Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя: "; wcin >> name; for (i=0; i<10; i+=2) { if (!wcscmp(name,numbers[i])) { wcout << L"Π’Π΅Π»Π΅Ρ„ΠΎΠ½ " << name << L": " << numbers[i+1] << L"\n"; break; } } if (i >= 10) { wcout << L"НичСго Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ\n"; } system("pause"); return 0; } 

If my memory serves me, str * functions do not like languages ​​other than English.

  • The str * functions cannot like * wchar_t, since they will quickly find a null character there. There should be no such problems with utf-8. - alexlz
  • here wchar_t to functions str. I'm talking about the fact that different implementations can work "very differently" with languages ​​other than English in the form of char characters. - Mirdin
  • Strange, if the topic is long beaten, why the old-timers did not immediately give a link to the correct answer. - Dith
  • @Dith, apparently the old-timers are simply fed up with fruitlessly offering different ways and arguing about their ease of use and portability. Do you know the correct answer? - avp