Thread: Compare input with array element

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    38

    Compare input with array element

    heya,

    sorry stupid question but:

    how can I compare my input with a string element of an array?

    EDIT: check post below for source code

    Hm obviously the "code" of
    strcmp(codes[i].country, code)
    is the issue. I just don't know how I could "parse" code to a string to compare the arraystring with the input code :/

    Thanks in advance!
    Last edited by coffee_cup; 12-03-2012 at 10:52 AM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Use a for loop:
    Code:
    int checkMatch() {
      int i = 0; 
      for ( i=0; i<sizeof codes/sizeof *codes; i++ ) {
        if ( strcmp( codes[i].country, code ) == 0 ) {
          // as before
          return i;
        }
      }
      return -1;
    }
    It's probably useful to return the index (i.e. i) in the checkMatch function in case your main function might want to do anything with it. -1 is a 'no match' value, as the -1th element doesn't exist.

    I'm also not entirely sure what your if statements with inputcount is meant to do...
    Last edited by twomers; 12-03-2012 at 10:12 AM. Reason: Missing {, and return -1

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Thx alot I added a loop now but it doesn't work yet See main post for new sourcecode

    Eventhough it should be a match, strcmp returns a -1

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Quote Originally Posted by twomers View Post
    I'm also not entirely sure what your if statements with inputcount is meant to do...
    This isn't the full code, I need inputcode as I'm comparing every single character if it's a number or a sign there probably are easier solutions though, I just didn't post the full source yet as I'm not finished

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Ah well I fear the issue lies within my messy code rather than the strcmp method itself

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <String.h>
    #include <stdbool.h>
    #define ZEICHEN 1024
    
    int inputcount = 0;
    char code[ZEICHEN];
    bool numeric = false;
    bool alpha = false;
    
        typedef const char* const CountryNameType;
        typedef const unsigned int DialingCodeType;
        struct country_code
        {
            CountryNameType country;
            DialingCodeType code;
        }
        codes[] =
        {
            {"Austria" , 43},
            {"Belgium", 32},
            {"Cyprus", 357},
            {"Czech Republic", 420},
            {"Germany", 49},
            {"Denmark", 45},
            {"Estonia", 372},
            {"France", 33},
            {"Finland", 358},
        };
    
    int len = sizeof(codes)/sizeof(int)/2;
    
    void statusCheck()
    {
        if (numeric && alpha)
        {
            printf("Unknown country name.\n");
            return;
        } else if (!numeric && alpha)
        {
            for (int i=0; i<len; i++)
            {
                if (strcmp(codes[i].country, code) == 0)
                {
                    printf("Match: %d\n", codes[i].code);
                    return;
                }
            }
        } else if (numeric && !alpha)
        {
            for (int i=0; i<len; i++)
            {
                if (codes[i].code == atoi(code))
                {
                    printf("Match: %s\n", codes[i].country);
                    return;
                }
            }
        }
    }
    void checkMethod()
    {
        numeric = false;
        alpha = false;
        inputcount = 0;
        printf("Enter dialing code or country name: ");
        while (1)
        {
            scanf("%c", &code[inputcount]);
            if (code[inputcount] != '\n')
            {
                inputcount++;
                if (isalpha(code[inputcount-1]))
                {
                    alpha = true;
                }
                else if (isdigit(code[inputcount-1]))
                {
                    numeric = true;
                }
            }
            else
            {
                statusCheck();
                checkMethod();
            }
        }
    }
    
    int main()
    {
        checkMethod();
        return 0;
    }
    Last edited by coffee_cup; 12-03-2012 at 10:49 AM.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are going out of bounds in the for in line 43. I guess you are computing variable len incorrectly.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    In line 55 a DialingCodeType (int) is being compared to a CountryNameType (char*). You'll need to look into atoi or similar functions to compare the two.

    It's going to be easier to do an scanf( "%s", code );, then check [FONT=Courier New]code[0][/FONT for alphanumericy rather than read in character by character.
    Last edited by twomers; 12-03-2012 at 10:53 AM.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    EDIT: nm that wasn't the issue...
    Last edited by coffee_cup; 12-03-2012 at 10:46 AM.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    thx for the help updated the source code

    the numeric input is working as it should now with atoi

    the string input still won't work though =/

    Maybe it really is because of the \n linebreak of the input?
    Last edited by coffee_cup; 12-03-2012 at 10:51 AM.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Edit: dblpost sry

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    You're probably over-complicating it too... this semi-pseudo code is a more direct route to the issue

    Code:
    const int len = sizeof codes /sizeof *codes;
    
    int main( void ) {
      int i = 0; 
    
      do {
        printf( "Enter country or code: " ); 
        scanf( "%s", code );
    
        if ( isdigit( code[0] ) ) {
          int num = atoi( code );
    
          for ( CODES RANGE )
            if ( CODES EQUAL ) 
              printf( "Match at %d.\n", i );
     
        } else {
          for ( CODES RANGE )
            if ( STRINGS EQUAL) 
              printf( "Match at %d.\n", i ); 
        }
    
      } while ( CONDITION );
    }

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    thanks a ton twomers

    small question: if I want to ensure that the full string consists of signs with no numbers in them, I'd still have to loop through every char with
    Code:
    for (int i=0; i<strlen(code); i++)
    {
      if ( isdigit( code[i] ) ) { ... }
    }
    to ensure that all characters are numbers, as "if ( isdigit( code[0] ) )" only checks the first character no?

    thx again

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Maybe you should test that all of the variables are of the same type, i.e something like.
    Code:
    int main( void ) {
      char *code;
      int j = 0; 
      char *codes[3] = { "abc", "123", "a12" }; 
    
      for ( j=0; j<3; j++ ) {
        int numDigit, numAlpha, len, i;
        code = codes[j];
    
        printf( "Testing %s: ", code ); 
    
        numDigit = 0;
        numAlpha = 0; 
        len = strlen( code ); 
    
        for ( i=0; i<len; i++ ) {
          if ( isdigit( code[i] ) ) numDigit ++; 
          if ( isalpha( code[i] ) ) numAlpha ++; 
        }
    
        if ( numDigit == len ) {
          printf( "All digit\n" ); 
    
        } else if ( numAlpha == len ) {
          printf( "All alpha\n" ); 
    
        } else {
          printf( "Mixture.\n" ); 
        }
      }
    
      return 0; 
    }
    Last edited by twomers; 12-03-2012 at 11:31 AM. Reason: Better example

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    Thanks alot for the explanation! I will rebuild my code once I got the root issue resolved, as I fear it would be the same even recoded:

    in the source code posted in #5:
    Code:
    if (codes[i].code == atoi(code))
    for the countrycode does work

    but
    Code:
    if (strcmp(codes[i].country, code) == 0)
    for the countryname won't

    I fear it's because of the linebreak \n which happens to be compared in the string. I kinda can't find a solution to that 8(

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    solution: added
    code[inputcount] = 0;
    to overwrite the \n. This probably isn't the best solution but working for now ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare user input and function with array and bubblesort
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 06-15-2012, 01:32 PM
  2. Using strncmp to compare input with string
    By lovelace in forum C Programming
    Replies: 9
    Last Post: 10-11-2011, 09:11 AM
  3. Replies: 60
    Last Post: 05-31-2010, 10:57 AM
  4. how to compare element in array
    By hlam in forum C++ Programming
    Replies: 7
    Last Post: 11-29-2003, 12:05 AM
  5. compare input doubles
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2002, 08:57 PM