By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

Problem in comparing string of integers.

P: 2
Is there any function which can compare first size and then inner elements.. I have used compare functions but that outputs the same.
Expand|Select|Wrap|Line Numbers
  1. string A="100001",B="9";
  2.  
  3.      if (A>B)
  4.      {
  5.          cout<<" ***A****";
  6.      }
  7.      else if (B>A)
  8.      {
  9.          cout<<"   *****B*****";
  10.      }
  11.      else if (B==A)
  12.      {
  13.          cout<<"*******same******* ";
  14.      }
  15.  
Thankyou!
Feb 27 '16 #1

✓ answered by AceInfinity

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.

Share this Question
Share on Google+
3 Replies


P: 12
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.
Feb 28 '16 #2

P: 2
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!
Feb 28 '16 #3

P: 12
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.
Feb 28 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.