recursion string error, why this is happening


#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
I am new to proggraming and having an error in return temp line. I don't know the cause of the error. The compiler says cannot convert std string to int in return. I saved the temp as name so why does it still have the error
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) temp is a string. But main() is - quite correctly - defined to return an int, not a string.

Why are you trying to return a string from main()? What are you trying to achieve by that? Do you understand what happens to the value returned from the main() function of a program?
Last edited on
Topic archived. No new replies allowed.