Thread: plz help me with simple string comparison.

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

    plz help me with simple string comparison.

    i have no idea why this isn't working.. plz plz help a guy out.


    Code:
        char monthNames[2][10] = {"jan", "feb"};
        char monthSelection[10];
    // printf(monthNames[0]);
        int check = 0;
        while(check != 1)
        {
            printf("Enter the month: (e.g. January ) ");
            gets(monthSelection);
            printf(monthSelection);
            printf(monthNames[0]);
    //        for (int i = 0; i < 2; i++)
    //        {
                if (monthSelection == monthNames[0])
                {
                    printf("correct");
                    check = 1;
                    break;
                }
    //        }
        }
    in the end i want the program to check to see if the input exists in the array. i was thinking of using a for loop to check every element of the array.. but i can't even get this script to work..

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should be using std::string to avoid these pitfalls.
    Code:
    std::string monthNames[2] = { "Jan", "Feb" };
    But wait... isn't this pure C code? Do you want C instead of C++?
    And don't use gets; use fgets (in case you are using C).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    this is a homework assignment.. the only header thats included is
    #include <stdio.h>

    does that support std::string?

    also is there a faster method of getting the same result?
    or better method.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you use C or C++?
    This is the C++ forum but your code is C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    well i guess it's c. It's a c++ class tho but they gave a c example to work with.. sorry i will post on the c board.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if you can use C++, you might as well learn it, if you're willing.
    std::cout instead of printf, std::cin or std::getline instead of gets and std::string instead of char.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    cool. but why isn't the comparison working lol

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will, if all strings are std::string. Comparing char* to char* won't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    MegaManZZ: To compare stings include <cstring> and use strcmp. If it returns 0 your strings are the same, something like:
    Code:
    if( strcmp(string1, string2) == 0)
        // Match found

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For char* and char* and C, yeah, but not necessary for C++, so the question is what approach you're willing to take?
    C way:
    Code:
    const char* mystr1 = "My first string";
    const char* mystr2 = "My second string";
    if (strcmp(mystr1, mystr2) == 0)
    	; // Match
    C++ way:
    Code:
    std::string mystr1 = "My first string";
    std::string mystr2 = "My second string";
    if (mystr1 == mystr2)
    	; // Match
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Its perfectly valid to use C style strings in C++. While there may be lots of benefits to using std::string somehow I doubt the OP cares.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So it is, but when learning C++, you should try to do it the "C++" way and no the "C+" way, yes?
    So unless the extra benefits of C-style strings are required, it's better to use the C++ way...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM