Thread: strcmp with numbers

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    strcmp with numbers

    Hi, I need to compare two numbers.

    If the numbers are not made out of the same number of digits strcmp() will return 0 meaning that they are equal.

    strncmp() will return the correct answer 1, meaning the first number is greater.

    Is there a work around with strcmp() that it returns the correct result too?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       
        int ret = strcmp("1 222 111", "1 222 11");
        cout << "Result: " << ret << "\n";
    
        ret = strncmp("1 222 111", "1 222 11", 9);
        cout << "Result: " << ret << "\n";
    
       
        system("pause");
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    First off why are you using the C-string functions in the first place? Why not just use C++ strings and then directly compare the strings?

    Second I can't reproduce your results because the program fails to compile on my system because you forgot to #include a required include file.

    Third after fixing the above issue I get the following output:

    Result: 1
    Result: 49
    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
        int ret = strcmp("1 222 111", "1 222 11");
        cout << "Result: " << ret << "\n";
    
        ret = strncmp("1 222 111", "1 222 11", 9);
        cout << "Result: " << ret << "\n";
    
        std::cout << "Are the strings equal: " << std::boolalpha << ("1 222 111" == "1 222 11") << std::endl;
        std::cout << "Are the strings equal: " << std::boolalpha << ("1 222 111" == "1 222 111") << std::endl;
    
    
        return 0;
    }
    The above produces the following on my system:

    Result: 1
    Result: 49
    Are the strings equal: false
    Are the strings equal: true

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Jim,

    Now, I get 1 too on strcmp(). I dont understand why I got 0 before.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What did you change?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Jim has made a rare error here, simply comparing the addresses of the string literals and not their "values":
    Code:
        std::cout << "Are the strings equal: " << std::boolalpha << ("1 222 111" == "1 222 11") << std::endl;
        std::cout << "Are the strings equal: " << std::boolalpha << ("1 222 111" == "1 222 111") << std::endl;
    As I'm sure he knows, to use C++ std::string comparison operators you need to convert the string literals to std::strings.
    Code:
        std::cout << "Are the strings equal: " << (std::string("1 222 111") == "1 222 11") << '\n';
    Only one string literal needs to be explicitly converted; the other will be converted implicitly.
    Since C++14, you can also say "1 222 111"s to make a std::string literal.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Quote Originally Posted by jimblumberg View Post
    What did you change?
    Just added the include files you suggested. But it compiles without them also and I get the good result 1.

    Anyway I realized that numbers cannot be compared as strings because they will be compared alphabetically like strings are compared.

    "600" will be bigger than "1000" because the first character is bigger.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    Anyway I realized that numbers cannot be compared as strings because they will be compared alphabetically like strings are compared.

    "600" will be bigger than "1000" because the first character is bigger.
    If you're dealing with non-negative integer string representations without leading 0s, then it is a simple matter of comparing string length first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    By your first example you need to include
    #include <string.h>
    After this change i've got 1 as return value of strcmp too.

    jimblumberg's exampe returns 49 of strncmp. That's the ASCII-Value of the character "1".

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by rusyoldguy View Post
    jimblumberg's exampe returns 49 of strncmp. That's the ASCII-Value of the character "1".
    That's because the first discrepancy in the two strings is between the last '1' of the first string and the terminating '\0' of the second string, the difference of their ascii codes being 49 since '1' is 49 and '\0' is 0.

    That it returns the difference is specific to your system since the standard does not guarantee that. It only guarantees a positive, negative, or zero value, with no other meaning than greater than, less than, and equal, respectively.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Quote Originally Posted by laserlight View Post
    If you're dealing with non-negative integer string representations without leading 0s, then it is a simple matter of comparing string length first.
    Thats true, I didnt think about this.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2017, 01:36 AM
  2. Replies: 6
    Last Post: 03-13-2016, 02:34 PM
  3. Replies: 10
    Last Post: 02-08-2012, 03:17 PM
  4. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  5. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM

Tags for this Thread