0
#include <bits/stdc++.h>

using namespace std;

int main() 
{
    string s("092282");
    cout << s[0];
    if (s[0] < (char)9)
    {
        cout << "yesss";
    }
}

In this text I am not able to understand how to compare string element which is numeric constant and a numeric.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
sparsh goil
  • 33
  • 1
  • 7

1 Answers1

0

To can compare characters with intergers but char c = '9' is encoded as integer using ASCII table standard. In this table character '9' is equal to 57. You can simply check this.

if('9' == (char)57)
    cout << "yes";  

output:

yes

Change your code as below:

int main() 
{
    string s("092282");
    cout << s[0] << endl;
    if (s[0] < '9')
    {
        cout << "yesss";
    }
}
Kuba
  • 123
  • 6
  • You **can** compare characters with integers. You have to be careful that the values you're dealing with are appropriate. And for the comparison `if ('9' == (char)57)` the cast is superfluous; `if ('9' == 57)` does the same thing. Both values will be converted to `int` for the comparison. – Pete Becker Dec 17 '18 at 16:23
  • Note that using ASCII is quite common, but not required. – Pete Becker Dec 17 '18 at 16:24
  • 1
    Actually just noticed that's the OP's bug not yours so no downvote – Lightness Races in Orbit Dec 17 '18 at 16:52