CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    46

    [RESOLVED] Problem with if

    Hi guys. I have one problem with if statement.
    Code:
    char MorzeLetters[20][6];
    for(int i=0; i<letter; i++)
    {
    	char temp[6];
    	strcpy_s(temp, 6, "-.--");
    	strcpy_s(MorzeLetters[i], 6, "-.--");
    	if(MorzeLetters[i] == temp)
    		strcat_s(result, 256, "y");
    }
    That if always fails and i cannot understand why? Hope you can help me...
    BTW i'm trying to write morze alphabet interpreter and in original code MorzeLetters will be set from string. This is just an example of my problem.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem with if

    if(MorzeLetters[i] == temp)

    You're comparing pointers, not data.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with if

    Quote Originally Posted by fire_ View Post
    Hi guys. I have one problem with if statement.
    Code:
    char MorzeLetters[20][6];
    for(int i=0; i<letter; i++)
    {
    	char temp[6];
    	strcpy_s(temp, 6, "-.--");
    	strcpy_s(MorzeLetters[i], 6, "-.--");
    	if(MorzeLetters[i] == temp)
    		strcat_s(result, 256, "y");
    }
    That if always fails and i cannot understand why?
    If you used string classes, then that code would work. Instead you decided to use low-level NULL terminated arrrays of char. Since arrays cannot be compared using ==, the only way to compare for equality is by comparing each character, one-by-one, in a loop. There is a function, strcmp(), that does this already.

    Why aren't you using a string class (this is C++), and instead using C-null terminated strings? Then the comparisons will be "natural" in the sense that you can use ==, <=, >=, !=, etc.
    Code:
    #include <string>
    
    using namespace std;
    
    string MorzeLetters[20];
    for(int i=0; i<letter; i++)
    {
        string temp = "-.--"
        MorzeLetters[i] = "-.--";
        if (MorzeLetters[i] == temp) { }
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2010
    Posts
    46

    Re: Problem with if

    That strcmp function works perfectly, thank you. I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Problem with if

    Quote Originally Posted by fire_ View Post
    I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...
    What kind of problems?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with if

    Quote Originally Posted by fire_ View Post
    That strcmp function works perfectly, thank you. I wasn't using string, because i have to put result to the edit box and earlier i had problems with putting string to edit box...
    That is because you did not use the string class properly.

    First, there is no string class that would not have a way to convert to a type that is compatible to an array of char (const char*). Luckily, std::string has the c_str() function to return a const char*. Did you use that function, or did you try and use std::string directly?
    Code:
    #include <string>
    
    void someFunc(const char *s)  // this takes a const char *
    {
    }
    
    int main()
    {
       std::string s;
       someFunc(s.c_str() );  // Line 1 is OK
       someFunc( s );  // Line 2 is an error
    }
    Did you try to do something like line 1 or line 2? I bet it was line 2.

    So there is no need to go and use low-level C functions to manipulate string data.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured