Thread: Compare input with array element

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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