Thread: please help with a string comparison.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    please help with a string comparison.

    i need to input a month and check to see if the month exists in the array.
    here is the code.

    Code:
      char monthNames[2][10] = {"jan", "feb"};
        int check = 0;
        while(check != 1)
        {
            printf("Enter the month: (e.g. January ) ");
            gets(pdate->month);
            //printf(pdate->month);
            //printf(monthNames[0]);
    //        for (int i = 0; i < 2; i++)
    //        {
                if (pdate->month == monthNames[0])
                {
                    printf("correct");
                    check = 1;
                    break;
                }
    //        }
        }


    the header im using is <stdio.h> and im getting rather frustrated. please tell me if there is a better way of accomplishing the same thing.. or plz just helpme make this code work.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, strings aren't compared with just ==. Instead, a library function called strcmp() is used:
    You'll want to look it up (I can't at the moment), but it's something like this iirc:

    Code:
    if(strcmp(pointer2string1, pointer2string2) == 0)
    /* strings are the same with 0, target string is greater if it returns 1, and target is less if it returns -1.
    
    DON'T quote me on which is -1 and which is 1, I don't do much programming with strings, in C.
     */
    You'll need to #include <string.h> to use strcmp.

    Edit: Also, printf doesn't work like this:
    Code:
    printf(monthNames[0]);
    
    What you want is either a literal string in double quotes: 
    
    printf("hello world!"); 
    
    or 
    
    printf("\n &#37;s ", monthName[0]);
    
    where the \n give you a newline, the %s tells printf that you want it to 
    print a string (%d or %i for integers), and finally, the data which corresponds 
    to the string printing you want done.
    Last edited by Adak; 02-18-2008 at 12:19 PM.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Also gets should not be used. See FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    this is a class lesson and the example uses gets??? dunno why but it does.. other then that everything worked fine!!

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MegaManZZ View Post
    this is a class lesson and the example uses gets???
    Then it is bad lesson that teaches bad practices
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's funny how schools in England forbids traditional games like conkers on grounds of safety, but students are exposed to this sort of insecure code. It's like the teachers at a motorcycling school riding without helmets, or metalwork classes not using safety-goggles.

    Yes, gets() does the job. And if you are lucky, you can drill a hole in a piece of metal without getting any of the metal in your eyes. But you don't want to take the risk.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM