Thread: Comparing strings

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Comparing strings

    Hey I got a question about comparing strings. I have a code similar to below:
    Code:
    typedef char * string;
    
    void main()
    {
    char choice[10]; printf("Enter number:\n"); scanf("%s", choice); int result = getnum(choice); printf("Number: %d", result);
    } int getnum(char choice[10]) {
    string numbers[] = {"zero","one", "two", "three", "four"}; int i; for(i=0;i<5;i++) {
    if(strcmp(numbers[i], choice) == 0) {
    int result = i; return result;
    }
    }
    }
    The purpose of the code above is basically to turn a string into a number. Here's the problem I'm having if the user enters a valid number (One of the numbers in the array string) then the result is correct however if the result is not in the array it still returns a "1" for some reason.

    Any reason for this? I'm new to C programming so excuse my amateur script
    Last edited by JohnDeon; 11-22-2011 at 06:48 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's actually undefined behavior. Since the user entered a string that matches none of the other options, the loop will finish completely and the function will end improperly without a return statement.

    Here's a look at what your compiler should be saying about the code:

    C:\Documents and Settings\User>gcc -c -Wall test.c
    test.c:4:6: warning: return type of 'main' is not 'int'
    test.c: In function 'main':
    test.c:7:1: warning: implicit declaration of function 'printf'
    test.c:7:1: warning: incompatible implicit declaration of built-in function 'pri
    ntf'
    test.c:8:1: warning: implicit declaration of function 'scanf'
    test.c:8:1: warning: incompatible implicit declaration of built-in function 'sca
    nf'
    test.c:9:1: warning: implicit declaration of function 'getnum'
    test.c: In function 'getnum':
    test.c:18:1: warning: implicit declaration of function 'strcmp'
    test.c:23:1: warning: control reaches end of non-void function
    The very last warning is what I talked about. But ... they are all important.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I'm using codeblocks and it shows me none of those warnings. But is there a way of making the function return a "-1" if the number is not valid? if I just put it at the end of the loop like so:

    Code:
    int getnum(char choice[10]){
    string numbers[] = {"zero","one", "two", "three", "four"};int i;for(i=0;i<5;i++){
    if(strcmp(numbers[i], choice) == 0){
    int result = i;return result;
    }else return -1;
    }
    }
    It's going to return -1 after each position it checks in the array. Any way of overcoming this?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes you can make the function return -1 if the number is not valid.

    Code:
    int getnum(char choice[10])
    {
       string numbers[5] = { /*....*/};
       int result = -1, i;
       
       for ( i = 0; i < 5; i++) {
          if (strcmp(numbers[i], choice) == 0) {
             result = i;
             break;
         }
       }
       return result;
    }
    Or anything logically the same will work.

    I'm not sure why your code is so hard to read. Please indent in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing two strings
    By winggx in forum C Programming
    Replies: 9
    Last Post: 03-30-2010, 10:17 AM
  2. comparing strings.
    By goran00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2008, 08:18 AM
  3. comparing strings
    By gammacad in forum C Programming
    Replies: 3
    Last Post: 06-15-2002, 06:07 PM
  4. Comparing strings...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-18-2002, 05:42 AM
  5. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 07:01 PM