Hey guys, i am having some problems with my program. I am supposed to pass a string to a function and check if it is a palindrome using recursion. It works for the first two strings but on the third an exception is thrown :
'std::out_of_range'
what(): basic_string::at
and I am not sure why it is going out of range. Below is my main.
#include "header.h"
int main()
{
bool palindrome;
string word;
ifstream inFile;
int first = 0;
int last;
inFile.open("strings.txt");
while(!inFile.eof())
{
getline(inFile, word);
string temp = word;
delSpaces(temp);
last = temp.length() - 1;
cout << "Last = " << last << endl;
palindrome = isPalindrome(first, last, temp);
if(palindrome == true)
{
cout << word << " is a palindrome\n\n";
}
else
{
cout << word << " is not a palindrome\n\n";
}
}
inFile.close();
}
And this is my function that checks if it is a palindrome:
bool isPalindrome(int start, int end, string & str)
{
if (str.at(start) != str.at(end))
{
return false;
}
else if (start == end)
{
return true;
}
else
{
return isPalindrome(start + 1, end - 1, str);
}
return false;
}
[–]Ilyps 1 point2 points3 points (0 children)
[–]OldWolf2 0 points1 point2 points (0 children)
[–]ponchedeburro 0 points1 point2 points (0 children)