recursion why i am having this problem

//I am new to programming and having a problem with this code, I want to return the string if read back-words is in fact a palindrome. I am having an error on the line with return temp. The error says that cannot convert string into an int return. Really confused about this can someone elaborate on this and tell me how to resolve this issue.


#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

bool isPal(string);

int main()
{

string name;

while (true)
{
string temp;//temp string

cout << "please enter the name or phrase that you would like to check if it is palindrome" << endl;
cin >> name;

for (int i = 0; i < name.length(); i++) //
{

if(isalnum(name[i]))//read each char and add to temp string (isalnum numbers and letters read)
{

// do i need add numbers
temp += name[i];

}




//then call tolower
temp = name;
//save ttemp to name
return temp;
}

if (isPal(name))
cout << name << " is in fact a palindrome" << endl;

else
cout << name << " is not a palindrome" << endl;
}

system("pause");
return 0;
}

bool isPal(string name)
{

int length = name.length(); // length of the string

//cout << "length " << length << " name: " << name << endl;


if (length <= 1)
{
return true;

}

if (length == 2)// at is for array and pointent ta begin and end
{
if (name.at(0) == name.at(1))
return true;

else
return false;
}

if (name.at(0) == name.at(name.length() - 1))
{
name.erase(name.end()-1); // earse last charcter from string
name.erase(name.begin()); // earse first charcter from string
return isPal(name); // ** recursion part
}


else
return false; // last one to check if all above aren't true
}
Last edited on
return exits a function. You have it in the middle of your main function. So at the end of your for loop you are basically trying to exit main by returning a string.
It works perfectly just removing the for loop inside main, probably some typo from old code. The following code compiles & works for me, reported without any formatting to be fair:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

bool isPal(string);

int main() {

string name;

while (true) {
string temp; //temp string

cout
<< "please enter the name or phrase that you would like to check if it is palindrome"
<< endl;
cin >> name;

if (isPal(name))
cout << name << " is in fact a palindrome" << endl;

else
cout << name << " is not a palindrome" << endl;
}

return 0;
}

bool isPal(string name) {

int length = name.length(); // length of the string

//cout << "length " << length << " name: " << name << endl;

if (length <= 1) {
return true;

}

if (length == 2) // at is for array and pointent ta begin and end
{
if (name.at(0) == name.at(1))
return true;

else
return false;
}

if (name.at(0) == name.at(name.length() - 1)) {
name.erase(name.end() - 1); // earse last charcter from string
name.erase(name.begin()); // earse first charcter from string
return isPal(name); // ** recursion part
}

else
return false; // last one to check if all above aren't true
}
Topic archived. No new replies allowed.