problem with custom made stoll() function

My apologies to L B who already showed me an easier way to do this... I'm doing this out of general interest only... :-) So the stoll() (string to long long) function didn't compile on my system so I tried making my own, which is supposed to do what's in its name, extract long long ints from a string (could be uLong, uShort, etc... I just started with long long). The function's second parameter is an index passed by reference, used to extract ALL the numbers in a string, one after the other. The third parameter is the base of the numbers you're looking for in the string, which is here defaulted to 10 for convenience. So here's the code, the problem is explained at the bottom of the post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

long long stringToLongLong(string myString, unsigned long long& index, unsigned short base = 10);

int main()
{
    string myLine;
    unsigned long long index = 0;

    vector<long long> myVector;

    getline(cin, myLine);

    cout << endl;

    while(index < myLine.size())
    {
        myVector.push_back(stringToLongLong(myLine, index));
    }

    for(std::vector<long long>::iterator iT = myVector.begin(); iT != myVector.end(); iT++)
    {
        cout << *iT << endl;
    }

    return 0;
}

long long stringToLongLong(string myString, unsigned long long& index, unsigned short base)
{
    long long beginningOfNumber = -1;
    long long returnValue = 0;

    bool negativeValue = false;

    // the magic numbers here are actually char values that represent '0' to '9' and 'a' to 'f' (both lower and upper case)
    if(base >= 2 || base <= 10)
    {
        while(index < myString.size() && (myString[index] < 48 || myString[index] > 48 + base - 1))
        {
            //if(index+1 < myString.size() && myString[index] == 45 && myString[index+1] >= 48 && myString[index+1] <= 48 + base - 1) negativeValue = true;

            index++;
        }

        if(index < myString.size())
        {
            beginningOfNumber = index;
        }
        while(index < myString.size() && myString[index] >= 48 && myString[index] <= 48 + base - 1)
        {
            index++;
        }
    }
    else if(base <= 16)
    {
        while(index < myString.size() && (myString[index] < 48 || myString[index] > 57) && (myString[index] < 65 || myString[index] > 65 + base - 11) && (myString[index] < 97 || myString[index] > 97 + base - 11))
        {
            //if(index+1 < myString.size() && myString[index] == 45 && (myString[index+1] >= 48 && myString[index+1] <= 57 || myString[index] >= 65 && myString[index] <= 65 + base - 11 || myString[index] >= 97 && myString[index] <= 97 + base - 11)) negativeValue = true;

            index++;
        }

        if(index < myString.size())
        {
            beginningOfNumber = index;
        }
        while(index < myString.size() && (myString[index] >= 48 && myString[index] <= 57 || myString[index] >= 65 && myString[index] <= 65 + base - 11 || myString[index] >= 97 && myString[index] <= 97 + base - 11))
        {
            index++;
        }
    }

    if(beginningOfNumber != -1)
    {
        for(unsigned long long lenght = index - beginningOfNumber; lenght > 0; lenght--)
        {
            if(myString[beginningOfNumber] == 48)
            {
                returnValue += 0*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 49)
            {
                returnValue += 1*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 50)
            {
                returnValue += 2*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 51)
            {
                returnValue += 3*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 52)
            {
                returnValue += 4*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 53)
            {
                returnValue += 5*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 54)
            {
                returnValue += 6*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 55)
            {
                returnValue += 7*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 56)
            {
                returnValue += 8*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 57)
            {
                returnValue += 9*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 65 || myString[beginningOfNumber] == 97)
            {
                returnValue += 10*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 66 || myString[beginningOfNumber] == 98)
            {
                returnValue += 11*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 67 || myString[beginningOfNumber] == 99)
            {
                returnValue += 12*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 68 || myString[beginningOfNumber] == 100)
            {
                returnValue += 13*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 69 || myString[beginningOfNumber] == 101)
            {
                returnValue += 14*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 70 || myString[beginningOfNumber] == 102)
            {
                returnValue += 15*pow(base, lenght-1);
            }

            beginningOfNumber++;
        }
    }

    if(negativeValue) return -1*returnValue;
    else return returnValue;
}


Everything seems to work fine, only for some reason certain numbers get pushed back in the vector with a value of value-1, and I have no idea why... I was wondering if anyone was interested in testing it to see what I mean, and hopefully tell me what the problem is...

Thanks in advance,
AeonFlux1212
Last edited on
here's an example, the string is on top and the numbers extracted are below...

kdfvn394857kdfjhvk95867dfjvkn1029dfvb0348bdv73473

394857
95866
1029
347
73472
it was the pow() function! apparently... :-)

AeonFlux1212
Last edited on
Topic archived. No new replies allowed.