2

I feel like I am missing something very obvious here...

In my program, I read user input from an edit box using GetWindowText(),
followed by this code:

if (x == L"R" || x == L"C" || x == L"L"){ n = 1; }
else{ n = 9; }

The debugger clearly says that x is L"R", but n is getting set as 9.

debugger

Is using if() in this situation wrong and should I use something else?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Pavel Pilař
  • 48
  • 1
  • 6
  • 2
    http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c – HerrJoebob Mar 27 '15 at 21:28
  • How did you declare the variable `x`, and how did you initialize it? In the bigger picture, what are you trying to do? – jww Mar 27 '15 at 21:37

2 Answers2

11

x is a wchar* pointer. The debugger is smart enough to show you the data that x is pointing at. The actual data is elsewhere in memory.

L"R" and the other values are string literals. They are implemented as pointers to wchar_t[] arrays stored in your app's read-only data segment.

Your if statement is comparing pointers to pointers, which will fail if they are not pointing at the same memory (in this case, they do not). To compare the actual data being pointed at, you need to use lstrcmpW() or similar function, eg:

if ((lstrcmpW(x, L"R") == 0) ||
    (lstrcmpW(x, L"C") == 0) ||
    (lstrcmpW(x, L"L") == 0))
{
    n = 1;
}
else
{
    n = 9;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

I'd suggest using CString (becomes CStringW in UNICODE build) instead of LPCWSTR (in case MFC is used). It does have operator==()

CString str;
m_DataEdit.GetWindowText(str);

if (str == _T("R") || str == _T("C") || str == _T("L"))
{ 
  n = 1; 
}        
else
{ 
  n = 9; 
}

LPCWSTR is simply a define for const WCHAR*. So you should use wcscmp function for comparison. It is much better if you use LPCTSTR, _tcscmp() and _T("") macro. This combination works well for both MBCS and UNICODE build.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • Its not clear that's what he wants to do. He may want the first letter from the `EditText`. (That *may* explain the downvote. But I would not have downvoted the answer, personally). – jww Mar 27 '15 at 21:38
  • This only works if the UI is using the MFC framework. It sounds like the OP is using plain Win32 API calls instead. The standalone `GetWindowText()` function does not fill a `CString`, it fills a `TCHAR[]` buffer instead. – Remy Lebeau Mar 27 '15 at 21:44
  • It is totally unclear from original post if he does or does not use MFC. So I made an assumption – Andrew Komiagin Mar 27 '15 at 21:47
  • 1
    `_tcscmp` is for comparing `TCHAR`. If `WCHAR` is in use, the function is `wcscmp` . – M.M Mar 27 '15 at 21:58
  • Correct. Just one addition: TCHAR is defined as WCHAR and _tcscmp becomes wcscmp in case of UNICODE build. – Andrew Komiagin Mar 27 '15 at 21:59
  • It is totally clear from the *winapi* tag, that a solution introducing an additional library dependency simply isn't a solution. The sloppiness in execution alone is downvote-worthy, I'm afraid. You certainly meant to use `CStringW` in place of an `LPCWSTR`. Mixing `LPCWSTR` with `_tcscmp` is equally wrong. – IInspectable Mar 27 '15 at 22:47
  • Thanks. Good catch. Made corrections based on your comments. – Andrew Komiagin Mar 28 '15 at 06:50