Thread: Strcmp

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Strcmp

    Hello, i am having a problem with the strcmp function. It seems that it always returns 0 even when the two strings are different. This is a snippet of the code i am using:

    Code:
    int results( char file[])
    {
         FILE  *results;
         char  line[BUFSIZ];
         if((results=fopen(file, "r")) ==NULL)
         {
            printf("Could not open file\n");
            return EXIT_FAILURE;
         }
         while( fgets(line, sizeof line, results) !=NULL )
         {
            sscanf(line,"%s %i %i %s %i %i", team1, &goal1, &behind1, team2, &goal2, &behind2);
            //printf("%s %i %i %s %i %i\n", team1, goal1, behind1, team2, goal2, behind2);
            {
               if(strcmp(teamnames[0],team1) == 0);
                  {
                   countgames++;
                  }
            }
         }
    printf("games = %i\n", countgames);
    fclose(results);
    return EXIT_SUCCESS;
    }
    The count should be at no more than 12 when i call the printf function, however it says the count has reached 59. What exactly have i done wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(strcmp(teamnames[0],team1) == 0);
    Remove the ; at the end.

    You've written this.
    Code:
    if(strcmp(teamnames[0],team1) == 0) { /*do nothing*/ }
    {
         countgames++;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Maxwell!

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thanks for your help Salem, I didn't realise it was that simple.
    Thankyou Adak

  5. #5
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Quote Originally Posted by Maxwell1993 View Post
    Thanks for your help Salem, I didn't realise it was that simple.
    Thankyou Adak
    Semicolons after if statements (and/or functions)... gets me quite often.
    Code:
    if (codeWorks( code) && !youCanDoSomethingBetter( code) )
    {
         beHappy();
    } else
    {
         fixCode( code);
    }

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by The Doctor View Post
    Semicolons after if statements (and/or functions)... gets me quite often.
    Extraneous semicolons on control statements are one of the beginner's banes of C, along with using = when you mean ==, leaving out a break from a switch case, forgetting to nul-terminate a string, leaving newlines in the input stream, precedence, pointers, proper EOF testing, buffer overflow, integer arithmetic, format specifiers, dynamic memory, ....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My strcmp
    By CStrong in forum C Programming
    Replies: 9
    Last Post: 06-25-2012, 07:53 AM
  2. strcmp help!!
    By apm in forum C Programming
    Replies: 14
    Last Post: 06-07-2007, 06:54 PM
  3. strcmp???
    By SvNo7 in forum C Programming
    Replies: 7
    Last Post: 12-30-2006, 04:34 PM
  4. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 01:32 PM
  5. strcmp
    By tat in forum C Programming
    Replies: 8
    Last Post: 12-05-2006, 12:07 AM