I am trying to compare two strings. When i enter two arguments on the command line 12 and 100, the flag1 should be set to 1 because
(argv[1] =) "100" > (junk2 = )"13". But this is not happening. So, how can i check that the input arguments entered on the command line are less then 13 and are only numbers.

I tried using atoi but did not work because argv[is] is not string. I need junk2 to be string becuase i am reading the value of junk2 from a file.

Please help.

string junk2 = "13";
int flag1 = 0;
for(int is=1; is<argc; is++)
        if(argv[is] > (junk2)
             cout << "The input " << argv[is] << " is incorrect" << "\n";
             flag1 = 1;


9 Years
Discussion Span
Last Post by Ancient Dragon

You need some way to convert strings to ints. One way is to use a stringstream, another way, after verifying that all characters of the string are digits and that there is one or more of them, is to use atoi(myStr.c_str())


argv is an array of character arrays so you can't use the > operator to compare a character array with a std::string. What you need to do is

1) junk should be an int, not a std::string
2) use strtol() to convert argv[is] to an int then compare the two ints.

>>I need junk2 to be string becuase i am reading the value of junk2 from a file.

So, read it as an int, not a string. If a string is an absolute must then convert it to an int using strtol() or stringstream.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.