strcmp

This is a discussion on strcmp within the C Programming forums, part of the General Programming Boards category; Hey, I have a huge assignment but there is a small part I'm confused about how to write it. I ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    104

    strcmp

    Hey, I have a huge assignment but there is a small part I'm confused about how to write it.

    I want to make a method

    Code:
    #include <string.h>
    
    int findMonth(char month[30]) {
       if(month.strcmp("JANUARY")
          return 1;
       elseif(month.strcmp("FEBRUARY")
          return 2;
       ...
    
    }
    
    what exactly is the name of the function of what I'm trying to accomplish. THanks in advance.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    The syntax

    month.strcmp( ... );

    is more common in C++, where you have OOP things like classes. In C, there are only functions. You have the right function in mind, by the way, but you need to figure out what it returns means.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    Alright, based on what I remember, it should be specifically, something like this?

    Code:
    if(month.strcmp("JANUARY")==0) {
       return 1;
    }
    i dont understand what youre saying about C++. does this mean that I should not be using this in C?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you comparing to? strcmp, even if you do manage to use it in a structure, you still need another argument.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    hmm i dont understand what you mean by needing another argument?

    well month is a char month[15] that i compare to January, Feb.. etc.

    oh the january compare method is just one line, not my whole method; i was just using it as an example to see if i have the correct syntax.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Soulzityr View Post
    hmm i dont understand what you mean by needing another argument?

    well month is a char month[15] that i compare to January, Feb.. etc.

    oh the january compare method is just one line, not my whole method; i was just using it as an example to see if i have the correct syntax.
    strcmp takes two arguments, two strings, if they are identical strcmp returns zero. You just have one argument. This is the correct syntax:

    Code:
    int strcmp(const char *s1, const char *s2);
    The dotsyntax is from C++, where month would be an object and strcmp a method.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Unless you HAVE to write a function that does all that, a better way would be to use an enumeration:

    Code:
    enum month{
        January,
        February,
        /***** so on */
        December
    };
    Then you can just do something like:

    Code:
    enum month this_month = January;

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    104
    ive never learned enumerations before so heheh i dont know what you mean by that

    so okay for syntax wise, would it be correct then, subzero, to put it this way

    Code:
    int compareAge(char month[15]) {
       if(strcmp(month,"JANUARY")==0) {
          return 1;
       }
       else if (strcmp(month,"FEBRUARY")==0) {
          return 2;
       }
       ...
    }

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Yes, that should work.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Except it's unusual to see an array size in a formal parameter. The value is pretty much ignored except that the compiler will recognize the use of an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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