M
M
Marcenary2021-01-18 01:25:03
Programming
Marcenary, 2021-01-18 01:25:03

Why does strcmp compare Cyrillic incorrectly?

There is a function that changes the field of the structure by comparing the entered field name with those that are. It works fine with English, but it doesn’t want to work with Russian, he considers the same word not the same. For example, the entered "name" compared with the "name" written in the strcmp program shows that the entered one is more, although this is the same word. And this problem in the project, as I noticed, in another project everything is compared correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-01-18
@Marcenary

Everything is simple (complicated) - the encoding of the entered characters does not match the encoding of the source code of the program.
You apparently write a console program under Windows - this is a typical problem for beginners here, because. in Russian Windows, 2 standard encodings cp1251 and cp866 are possible in the console, and cp866 is used by default (the default is when you just run cmd.exe). You can check the currently installed console encoding from the console itself with the chcp command, and you can also change the encoding with it.
The encoding of the source code can be anything, except for you, no one knows it.
For simplicity, in order for the program to work for you, recode the sources to cp866 and rebuild it.
But this will only work as long as you run the program on Russian Windows and as long as the console encoding in it is cp866.
The normal approach in this case is:
1. write the source code in UTF8,
2. find out the current console encoding,
3. before outputting any text, re-encode the text from UTF8 to the console encoding,
4. when entering text from the console, re-encode from the console encoding to UTF8, and only after that, you can perform some actions with the text, for example, strcmp().
5. in the code to store strings, use wchar_t and wstring, not char and string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question