if (argv[4]<argv[3])

Hi,
knowing that my main program starts with int main(int argc, char *argv[]) and that my arguments are decimal numbers, how can I easily (without entering too many more lines of code) interpret them (under their char *argv[] form) as such?

Purpose:
Eventually I'd like to evaluate a comparison such as: (argv[4]<argv[3])
(like in an if cell)

Thank you very much,

Sky
With C++11, you can use std::stoi to convert the arguments to int, e.g.:
if (stoi(argv[4])<stoi(argv[3]))
Without C++11, you can use boost::lexical_cast or std::stringstream (or atoi).
Thank you very much.
It seems to be working with stringstream, but I'm now noticing that I'd need the absolute value of one of this decimal numbers, do you know how I can do that (beside abs from <math>) ?
Thank you
I resolved this.
But no, actually I just noticed that stringstream(argv[4])>stringstream(argv[3]) does not compute. I don't know why, but it doesnt...
I'll try some of the others.
You probably need something like this (sans appropriate headers):

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char* argv[])
{
    istringstream ss3(string(argv[3]));
    istringstream ss4(string(argv[4]));
    int i3;
    int i4;
    ss3 >> i3;
    ss4 >> i4;
    if(i4 > i3)
    {
        ...
    }


Personally, I would prefer using atoi:
1
2
3
4
5
6
7
8
9
int main(int argc, char* argv[])
{
    int i3 = atoi(argv[3];
    int i4 = atoi(arv[4];

    if(i4 > i3)
    {
        ...
    }
Topic archived. No new replies allowed.