Thread: string compare

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    string compare

    i want the user to enter 50 names. i want to see if there is a match using strcmp but it wont work. any suggestions?

    Code:
    #include <stdio.h>
    #include<string.h>
    
    
    int main()
    {
        char string[50];
        char strcmp[50];
        int i;
        printf("enter 50 names\n");
        for(i=1;i<=50;i++)
        {
            scanf("%s",&string);
        }
        strcmp();
    
    
        if(strcmp==0)
        {
            printf("we have a match");
        }
        return 0;
    }

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Code:
    char strcmp[50];
    Code:
    strcmp();
    Code:
    if(strcmp==0)
    Seriously? What the hell are you asking the computer to do on these lines?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    The first example I pulled out - you're calling a variable with the same name as the function. Your compiler should have gone mad about this and put up lots of errors.
    The second example - you're calling strcmp with no parameters at all. What's it supposed to be comparing to what? You also don't do ANYTHING with the return value of it. Your compiler should have gone mad about this and put up lots of errors.
    The third example - you're calling strcmp with no parameters at all again, but at least you do try to do something with the results. However, guess what, Your compiler should have gone mad about this and put up lots of errors!


    Even if you fix those errors, your program does basically nothing at the moment. Your scanf is completely ridiculous and reads in 50 strings into the same variable, that can only hold one of length 50 (without bothering to check their length, etc. either), each time overwriting the previous.

    Even if you fix that and enter 50 names, you then do ONE check of two strings (which two I have no idea, neither does your compiler, because of the above problems). Surely you would need to check the new name against ALL the names previously entered ?

    You haven't even tried at all to compile this, let alone write it properly.
    Last edited by ledow; 03-02-2012 at 09:09 AM.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by ledow View Post
    The first example I pulled out - you're calling a variable with the same name as the function. Your compiler should have gone mad about this and put up lots of errors.
    The second example - you're calling strcmp with no parameters at all. What's it supposed to be comparing to what? You also don't do ANYTHING with the return value of it. Your compiler should have gone mad about this and put up lots of errors.
    The third example - you're calling strcmp with no parameters at all again, but at least you do try to do something with the results. However, guess what, Your compiler should have gone mad about this and put up lots of errors!


    Even if you fix those errors, your program does basically nothing at the moment. Your scanf is completely ridiculous and reads in 50 strings into the same variable, that can only hold one of length 50 (without bothering to check their length, etc. either), each time overwriting the previous.

    Even if you fix that and enter 50 names, you then do ONE check of two strings (which two I have no idea, neither does your compiler, because of the above problems). Surely you would need to check the new name against ALL the names previously entered ?

    You haven't even tried at all to compile this, let alone write it properly.
    ye basically everything i wrote there is garbage ha i only read strcmp's today and gave it a shot. suprisingly there were like 3 errors. but your complete enialation of my programme actually had me laughing at how studiply it is written ha "your scanf fucnction is completely ridiculous".thanks anyway

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You will need a 2d array if you want to enter in 50 names ex. char names[50][50];

    then you can use a loop to enter in the names

    for(i=0;i<50;i++)
    scanf("%s",names[i]);
    Last edited by camel-man; 03-02-2012 at 10:41 AM.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    oh yes ill try that tanks

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Somethings here...

    1. You cannot have a variable name the same as a function name. The compiler will not know how to handle/or work with them.
    2. Look up how strcmp() is used, strcmp(3): compare two strings - Linux man page

    And follow what ledow has said.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    i just dont know how to get the user to enter several names and then compare them using arrays

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Compare
    By shruthi in forum C Programming
    Replies: 2
    Last Post: 02-09-2012, 09:27 AM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. need help in string compare
    By afzan in forum C++ Programming
    Replies: 13
    Last Post: 03-31-2005, 07:05 AM
  4. string compare
    By IceBall in forum C Programming
    Replies: 4
    Last Post: 10-12-2003, 05:26 PM
  5. need help on string compare
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-07-2002, 08:55 PM