how can i make a condition with const wchar_t*?I have tried
const wchar_t* password
if (L"xyz" == password)
{...}
but it's not working
thank you
how can i make a condition with const wchar_t*?I have tried
const wchar_t* password
if (L"xyz" == password)
{...}
but it's not working
thank you
When you are doing L"xyz" == whatever
(or even the non-wide version, "xyz" == whatever
) you are comparing pointers, not string contents.
E.g. say your variable password
is stored at location 0x00100000
in memory, and the constant L"xyz"
is stored at location 0x00200000
. When you perform the equality test with the line L"xyz" == password
what it is actually comparing is 0x00100000 == 0x00200000
, which is obviously never going to be true.
What you need to do is use the library function wcscmp
to compare wide-character strings. However, since this is tagged as C++, you should be using std::wstring
and the overloaded operator ==
unless you have a reason to be using C strings. That may be where you're confused: when using std::string
or std::wstring
you can use ==
to compare strings because those classes overload the ==
operator so this code works just fine:
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::wstring wstr = L"Hello World";
if(wstr == L"Hello World")
{
std::wcout << wstr << std::endl;
}
return 0;
}
As a side note, you really shouldn't be using constants to store passwords in your program unless they're encrypted somehow.