Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By oogabooga

Strcmp

This is a discussion on Strcmp within the C Programming forums, part of the General Programming Boards category; Hello, i am having a problem with the strcmp function. It seems that it always returns 0 even when the ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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++;
    }
    qny likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, ....
    Matticus likes this.
    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, 03:34 PM
  4. What does strcmp actually do?
    By Brewer in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 12:32 PM
  5. strcmp
    By tat in forum C Programming
    Replies: 8
    Last Post: 12-04-2006, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21