0

I know this is a repeated question, and I a have looked at a lot of answers for it but none have really been able to help me so far.

I need to be able to pass various strings into two methods one returns a double and the other a int. The main problem is that I need strict error checking on both methods so that if I pass a string that dose not contain a number and only a number the method does not make a conversion. As I said I've seen a few solutions but the only good one I've seen (that was able to follow) was using Boost which I do not want to use. As for the answer I was not able to follow here is a part of it copied from

How to parse a string to an int in C++?

The best solution

Fortunately, somebody has already solved all of the above problems. The C standard library contains strtol and family which have none of these problems.

enum STR2INT_ERROR { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };

STR2INT_ERROR str2int (int &i, char const *s, int base = 0)
{
    char *end;
    long  l;
    errno = 0;
    l = strtol(s, &end, base);
    if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
        return OVERFLOW;
    }
    if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
        return UNDERFLOW;
    }
    if (*s == '\0' || *end != '\0') {
        return INCONVERTIBLE;
    }
    i = l;
    return SUCCESS;
} 

If anyone can explain it a little more I think it is the answer Im looking for I just cant get enough of a grasp of what its doing so that i can apply the idea to my code.

Community
  • 1
  • 1
James Thompson
  • 245
  • 1
  • 6
  • 13

3 Answers3

1

Using strtol (and strtod for doubles) sounds like the right approach. The implementation that you quoted flows straight from the documentation for strtol:

  • It sets errno to zero before the call (strtol does not change errno on success)
  • It examines errno upon return of strtol to see if an error has been signaled
  • The final condition checks that the input is not empty, and that the entire input has been consumed by strtol.

An implementation for doubles would look the same, except you'd use strtod.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I still don't know how to call it. I have no idea what the middle peram is. what i need to do is something like this string input; int example = strtol(input,?,0) – James Thompson Feb 10 '13 at 01:50
  • @JamesThompson The first pointer obviously points to the beginning of the input; upon return, a pointer pointed to by the second parameter will be set to the position in the input string where the consumed portion of the input has ended. You know that all input has been consumed if the second parameter points to the null terminator of the original string. – Sergey Kalinichenko Feb 10 '13 at 04:05
1

As a learning exercise, you might want to try to write this on your own. I suggest starting with some examples. You might want to start by getting a firm grasp on how numbers are represented in writing. What does "42" mean? In other words, what does the 2 represent and what does the 4 represent? Similarly, what does "1000000" mean? Once you get a grasp on these concepts, write down the steps (in English) you would take to convert a string into an integer. From there, start converting it into code. If you get stuck anywhere along the way, feel free to come back with more questions.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

I re-worded this question in another location and finally got the answer I was looking for. For any wondering here is the link:

Basics of strtol?

Community
  • 1
  • 1
James Thompson
  • 245
  • 1
  • 6
  • 13