Hi,

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;
         }
}

Thanks

Dani AI

Generated

The unexpected result comes from comparing the textual form of numbers, not their numeric values. Text comparison compares characters left-to-right, so "100" is lexicographically less than "13" ('1'=='1' then '0'<'3'). and are correct that you must convert the inputs before comparing, but do the conversion robustly (validate digits, handle signs/overflow, and choose a conversion routine appropriate to your C++ version).

A safe, modern pattern (C++17+) is: validate that the string contains only an optional sign and digits, then parse with std::from_chars (non-throwing, precise, and fast). Example validator + parser:

#include <charconv>
#include <string>
#include <cctype>

bool parse_integer(const std::string& s, long long &out) {
    if (s.empty()) return false;
    size_t i = 0;
    if (s[0] == '+' || s[0] == '-') ++i;
    for (; i < s.size(); ++i)
        if (!std::isdigit(static_cast<unsigned char>(s[i]))) return false;
    auto res = std::from_chars(s.data(), s.data() + s.size(), out);
    return res.ec == std::errc() && res.ptr == s.data() + s.size();
}

Use the function to convert each argv string to a numeric type and then compare the numeric values. Important cautions: cast to unsigned char before std::isdigit, std::from_chars does not skip whitespace and reports overflow via its error code, and older code may use std::stoll with try/catch instead. See std::from_chars and std::stoi for details: std::from_chars and .

Recommended Answers

All 2 Replies

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 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.