Asked  7 Months ago    Answers:  5   Viewed   37 times

I've had quite a bit of trouble trying to write a function that checks if a string is a number. For a game I am writing I just need to check if a line from the file I am reading is a number or not (I will know if it is a parameter this way). I wrote the below function which I believe was working smoothly (or I accidentally edited to stop it or I'm schizophrenic or Windows is schizophrenic):

bool isParam (string line)
    if (isdigit(atoi(line.c_str())))
        return true;

    return false;



The most efficient way would be just to iterate over the string until you find a non-digit character. If there are any non-digit characters, you can consider the string not a number.

bool is_number(const std::string& s)
    std::string::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it)) ++it;
    return !s.empty() && it == s.end();

Or if you want to do it the C++11 way:

bool is_number(const std::string& s)
    return !s.empty() && std::find_if(s.begin(), 
        s.end(), [](unsigned char c) { return !std::isdigit(c); }) == s.end();

As pointed out in the comments below, this only works for positive integers. If you need to detect negative integers or fractions, you should go with a more robust library-based solution. Although, adding support for negative integers is pretty trivial.

Tuesday, June 1, 2021
answered 7 Months ago

Try this:

// using System.CodeDom.Compiler;
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
if (provider.IsValidIdentifier (YOUR_VARIABLE_NAME)) {
      // Valid
} else {
      // Not valid
Monday, August 2, 2021
answered 4 Months ago

You can use this to try and parse it:


Then check AddressFamily which

Returns System.Net.Sockets.AddressFamily.InterNetwork for IPv4 or System.Net.Sockets.AddressFamily.InterNetworkV6 for IPv6.

EDIT: some sample code. change as desired:

    string input = "your IP address goes here";

    IPAddress address;
    if (IPAddress.TryParse(input, out address))
        switch (address.AddressFamily)
            case System.Net.Sockets.AddressFamily.InterNetwork:
                // we have IPv4
            case System.Net.Sockets.AddressFamily.InterNetworkV6:
                // we have IPv6
                // umm... yeah... I'm going to need to take your red packet and...
Sunday, August 8, 2021
answered 4 Months ago

To print an unsigned __int64 value in Visual C++ 6.0 you should use %I64u, not %lli (refer to this page on MSDN). %lli is only supported in Visual Studio 2005 and later versions. So, your code should be:

sprintf(buf, "%I64u, %s, %s", l, t1, t2);
Sunday, October 3, 2021
answered 2 Months ago

You should try to change the output format from ascii to unicode in vld.ini.

Your stack information must have unicode caracters that cannot be converted to ascii.

If VLD is compiled in debug, this results in assertions.

See this related topic on VLD web site:

I posted an alternative to changing vld.ini in a comment (need to modify vld sources and recompile).

Wednesday, November 10, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :