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: