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