Problem in comparing string of integers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ibrahimijc
    New Member
    • Feb 2016
    • 2

    Problem in comparing string of integers.

    Is there any function which can compare first size and then inner elements.. I have used compare functions but that outputs the same.
    Code:
    string A="100001",B="9";
    
         if (A>B)
         {
         	cout<<" ***A****";
    	 }
    	 else if (B>A)
    	 {
    	 	cout<<"   *****B*****";
    	 }
    	 else if (B==A)
    	 {
    	 	cout<<"*******same******* ";
    	 }
    Thankyou!
  • AceInfinity
    New Member
    • Apr 2013
    • 12

    #2
    Unless the > and == operators are overloaded, what exactly do you expect them to do? They are strings. What would you expect "Cat" < "Dog" to evaluate to? They are only defined for numeric value types mainly.

    Comment

    • ibrahimijc
      New Member
      • Feb 2016
      • 2

      #3
      Well I have used another method. And the thing which I was asking was that it is giving 9 > 10001 . which is not the desired result.

      Thank You!

      Comment

      • AceInfinity
        New Member
        • Apr 2013
        • 12

        #4
        You'll have to convert the strings to integers to use those operators, or overload the operators for the strings which will do the conversion or calculate the numbers based on the characters yourself.

        Comment

        Working...