-1

I am using stoi in one of my functions with the intent of converting a string of numbers into an integer. I was about halfway through a homework problem but then I ran into this. Oddly, if the number has an even number of characters, stoi only converts the first half. Any help would be greatly appreciated!

Code:

#include <fstream>
#include <vector>
#include <string>
using namespace std;
string start;
string endD;
int sDigit;
int eDigit;
int i;
vector<int> palindromes;

void construct(int layer, int digits, string prev)
{
    string temp = prev;
    if(layer > (digits % 2) + digits/2)
    {
        short a = (short) digits/2;
        for(int i = a; i >= 0; i--)
        {
            if(i == a && digits % 2 == 1)
            {
                continue;
            }
            else
            {
                temp.push_back(temp[i]);
            }
        }
        cout << temp << " " << stoi(temp) << endl; // Output is here
        palindromes.push_back(stoi(temp));
    }
    else if(layer == 1 && digits == sDigit)
    {
        for(int i = start[0] - '0'; i < 10; i++)
        {
            temp[0] = i + '0';
            construct(layer + 1, digits, temp);
        }
    }
    else if(layer == 1 && digits == eDigit)
    {
        for(int i = '1'; i <= endD[0]; i++)
        {
            temp[0] = i;
            construct(layer + 1, digits, temp);
            temp = prev;
        }
    }
    else if(layer == 1)
    {
        for(int i = 1; i < 10; i++)
        {
            temp[0] = '0' + i;
            construct(layer + 1, digits, temp);
            temp = prev;
        }
    }
    else
    {
        for(int i = 0; i < 10; i++)
        {
            temp.push_back(i + '0');
            construct(layer + 1, digits, temp);
            temp = prev;
        }
    }
}

int main()
{
    int startD, endDD;
    cin >> startD >> endDD;
    start = to_string(startD);
    endD = to_string(endDD);
    int tempS = startD;
    int tempE = endDD;
    while(tempS != 0)
    {
        tempS /= 10;
        sDigit++;
    }
    while(tempE != 0)
    {
        tempE /= 10;
        eDigit++;
    }
    for(int i = sDigit; i <= eDigit; i++)
    {
        construct(1, i, "x");
    }
    for(int i = 0; i < palindromes.size(); i++)
    {
        //cout << palindromes[i] << endl;
    }
}```

Input: 1 1000
Output:

Iscoconut
  • 1
  • 1
  • 2
    Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman May 28 '20 at 20:36
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl May 28 '20 at 20:41

1 Answers1

0

Your code has undefined behavior because in this line

temp.push_back(temp[i]);

you are accessing temp out-of-bounds. You can see this by adding a line

std::cout << "check " << i << " " << temp.size() << "\n";

just before that line.

Output will be (see here):

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
check 1 1
check 0 2
1
...

When the size is 1 the last valid index is 0. The problem is not with stoi but with the logic of your algorithm.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185