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:
- Character and string literals will have the same encoding as the file.
- External data sources (DB, files, network, etc.) - you need to specify the appropriate encoding and / or convert manually if necessary.
- 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