0

C++ strigs

how string comparisons work in C++ for example:std::cout<<("Annie">"Andy"); why this gives 0 when i executed my program

6th Aug 2022, 10:48 AM
selim sultan çorbacı
selim sultan çorbacı - avatar
4 Answers
+ 3
Can't compare that way now. You should understand that "Annie" and "Andy" are constant char literal in memory, those are not string (as in std::string). If you want to compare constant char literals, then go with strcmp() from <cstring> which handles C-String including such constant char literals. http://www.cplusplus.com/reference/cstring/strcmp/ On the other hand, if those two were actual std::string objects, then you can do comparison by using comparison operators (before C++20), https://en.cppreference.com/w/cpp/string/basic_string/operator_cmp Or, if you rather want the comparison to calculate such a value usable by sorting algorithms, you can use std::string::compare() function. https://en.cppreference.com/w/cpp/string/basic_string/compare
6th Aug 2022, 11:51 AM
Ipang
+ 1
That wrote number because you ddin't set the flag std::boolalpha. If it's true the program will write a boolean as "true" or "false", if its not it will write a number.
6th Aug 2022, 11:57 AM
Jordan Coppengaghen
Jordan Coppengaghen - avatar
0
i didn't understand at all. can you explain me basically
6th Aug 2022, 1:34 PM
selim sultan çorbacı
selim sultan çorbacı - avatar
0
Set flag like this: Std::boolalpha=false; So then Cout << true <<endl; Will write 1 in console If you set it like Std::boolalpha = true; That will write exactly "true" in console, got it?
6th Aug 2022, 2:07 PM
Jordan Coppengaghen
Jordan Coppengaghen - avatar