Leetcode is returning outside the range of representable values of type int error

  c++, runtime

I am solving a problem for converting a string to integers and there I am stuck in a problem where leetcode is returning this error "runtime error: 1e+10 is outside the range of representable values of type ‘int’". The code that I am running is mentioned below.

class Solution {
public:
    int myAtoi(string s) {
    string tempString;
    bool is_space = true;
    int n = 1;
    int integer = 0;
    char numbers[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    for (int i = 0; i < s.length(); i++) {
        if (!is_space && s[i] == ' ') {
            break;
        }
        if (s[i] != ' ') {
            is_space = false;
        }
        if (!is_space) {
            if (s[i] == '-') {
                n = -1;
                continue;
            }
            tempString += s[i];
        }
    }

    string tempString2 = tempString;
    int j = tempString.length() - 1;
    for (int i = 0; i < tempString.length(); i++) {
        tempString2[j] = tempString[i];
        j--;
    }

    for (int i = tempString2.length() - 1; i >= 0; --i) {
        for (int j = 0; j <= 9; j++) {
            if (tempString2[i] == numbers[j]) {
                integer += int(j) * int(pow(10, i));
                break;
            }
        }
    }

    return integer * n;
}

};

Source: Windows Questions C++

LEAVE A COMMENT