locked
question about check box in MFC RRS feed

  • Question

  • hello i trying to make program ( name info )

    getting information from user and store it in 2 difrent places

    but I have small problem with check boxes

    I have 3 difrent country name

    depends on the user entrance I wanna turn on the specific check box and store user information in their country folder

    I used if/else ...

    but at the end just 1 of check boxes is working

    any hint ?!

    I'm sorry for my bad English

    const wchar_t* CountryNameForCharPointer=(( const wchar_t* )pStrCountry);     //////capital and small letters

     if (_wcsicmp(L"Armenia", CountryNameForCharPointer))
     {
      m_Armenia.SetCheck(1);
      ;
     }
     else  if  (_wcsicmp(L"Iran", CountryNameForCharPointer))
     {
      m_Iran.SetCheck(1);
      
     }
     else  if (_wcsicmp(L"Russia", CountryNameForCharPointer))
     {
         m_Russia.SetCheck(1);

     }

    

    


    • Edited by Emad_shm Saturday, April 2, 2016 8:07 PM
    Saturday, April 2, 2016 8:07 PM

Answers

  • If you want to do a case insensitive test for string equality you need to test the return value of _wcsicmp against 0.  See https://msdn.microsoft.com/en-us/library/k59z8dwe.aspx

    Why are you using a (const wchar_t*) cast on pStrCountry?


    The reason only 1 box gets checked is that the code will check the box for the first "if" condition that is satisfied and skip the rest.

    • Edited by RLWA32 Saturday, April 2, 2016 8:26 PM
    • Proposed as answer by Hart Wang Monday, April 4, 2016 6:57 AM
    • Marked as answer by Hart Wang Monday, April 18, 2016 9:38 AM
    Saturday, April 2, 2016 8:24 PM

All replies

  • If you want to do a case insensitive test for string equality you need to test the return value of _wcsicmp against 0.  See https://msdn.microsoft.com/en-us/library/k59z8dwe.aspx

    Why are you using a (const wchar_t*) cast on pStrCountry?


    The reason only 1 box gets checked is that the code will check the box for the first "if" condition that is satisfied and skip the rest.

    • Edited by RLWA32 Saturday, April 2, 2016 8:26 PM
    • Proposed as answer by Hart Wang Monday, April 4, 2016 6:57 AM
    • Marked as answer by Hart Wang Monday, April 18, 2016 9:38 AM
    Saturday, April 2, 2016 8:24 PM
  • Hello,

     Personal, when IF/ELSE is concerned, I prefer the SWITCH/CASE. Mainly because I can

    go back and make addition with minimal code rebuilds. And IF/ELSE can get messy. But

    this is just my opinion.

    if (_wcsicmp(L"Armenia", CountryNameForCharPointer))
    {
    	m_Armenia.SetCheck(1);
    }
    else
    	if  (_wcsicmp(L"Iran", CountryNameForCharPointer))
    	{
    		m_Iran.SetCheck(1);
    	}
    	else
    		if (_wcsicmp(L"Russia", CountryNameForCharPointer))
    		{
    			m_Russia.SetCheck(1);
    		}
    // Versus
    
    switch( CountryNameForCharPointer )
    {
    	case L"Armenia":
    		m_Armenia.SetCheck(1);
    	break;
    
    	case L"Iran":
    		m_Iran.SetCheck(1);
    	break;
    
    	case L"Russia":
    		m_Russia.SetCheck(1);
    	break;
    }

    Thanks :)

    Sunday, April 3, 2016 1:23 AM
  • // Versus
    switch( CountryNameForCharPointer )
    {
    	case L"Armenia":
    		m_Armenia.SetCheck(1);
    	break;
    
    	case L"Iran":
    		m_Iran.SetCheck(1);
    	break;
    
    	case L"Russia":
    		m_Russia.SetCheck(1);
    	break;
    }

    Thanks :)

    The above code is not good advice.  You can't switch on string values in native C++ used with MFC
    Sunday, April 3, 2016 10:47 AM
  • Personal, when IF/ELSE is concerned, I prefer the SWITCH/CASE. Mainly because I can go back and make addition with minimal code rebuilds. And IF/ELSE can get messy. But this is just my opinion.

    Unfortunately, C++ cannot switch on strings.


    David Wilkinson | Visual C++ MVP

    Sunday, April 3, 2016 10:52 AM