palindrome checker, weird return types.

This is for an assignment, its to check if a palindrome is true or not. If I run this in debug mode and follow through it does work, although the return values are something I have never seen before, they appear to be random numbers between 1-200 true or false, what am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

bool isPalindrome(string, int, int);

int main()
{

	string palindrome = "taco cat";

	for (int i = 0; i < palindrome.length(); i++)
		if (palindrome [i] == ' ') 
			palindrome.erase(i, 1);

	int first = 0;
	int last = palindrome.length() - 1;

	bool test = (isPalindrome(palindrome, first, last));
		cout << test;

}

bool isPalindrome(string palindrome, int forward, int backward)
{
	if (palindrome[forward] != palindrome[backward])
		return (false);
	if (forward >= palindrome.length() - 1)
		return (true);
	isPalindrome(palindrome, ++forward, --backward);
}
Your problem is that your function only returns a bool if one of the if statements are true. I debugged your program and they never become true, added a few lines for debugging purposes -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string> // don't forget to include string...
using namespace std;

bool isPalindrome(string, int, int);

int main()
{

	string palindrome = "taco cat";

	for (int i = 0; i < palindrome.length(); i++)
		if (palindrome[i] == ' ')
			palindrome.erase(i, 1);

	int first = 0;
	int last = palindrome.length() - 1;

	bool test = (isPalindrome(palindrome, first, last));
	cout << test;

	system("pause");
}

bool isPalindrome(string palindrome, int forward, int backward)
{
	if (palindrome[forward] != palindrome[backward])
	{
		return (false);
		cout << "Statement 1 is true" << endl;
	}
		
	if (forward >= palindrome.length() - 1)
	{
		return (true);
		cout << "Statement 2 is true" << endl;
	}
		
	isPalindrome(palindrome, ++forward, --backward);
	cout << "hello";
}
You added the debug cout under the returns, if you add them above they do return true and false but yet I still get the strange return type values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 #include <iostream>
#include <string> // don't forget to include string...
using namespace std;

bool isPalindrome(string, int, int);

int main()
{

	string palindrome = "taco cat";

	for (int i = 0; i < palindrome.length(); i++)
		if (palindrome[i] == ' ')
			palindrome.erase(i, 1);

	int first = 0;
	int last = palindrome.length() - 1;

	bool test = (isPalindrome(palindrome, first, last));
	cout << test;

	system("pause");
}

bool isPalindrome(string palindrome, int forward, int backward)
{
	if (palindrome[forward] != palindrome[backward])
	{
		cout << "Statement 1 is true" << endl;
		return (false);
		
	}

	if (forward >= palindrome.length() - 1)
	{
		cout << "Statement 2 is true" << endl;
		return (true);
		
	}

	isPalindrome(palindrome, ++forward, --backward);
	cout << "hello";
}
Last edited on
You added the debug cout under the returns

Yes I did my bad. When it executes the return true; it doesn't exit the function, it instead continues to print out hello on the screen, this smells like undefined behaviour to me, Im not 100% sure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string, int, int);

int main()
{
	string palindrome = "taco cat";

	for (int i = 0; i < palindrome.length(); i++)
		if (palindrome [i] == ' ')
			palindrome.erase(i, 1);

	int first = 0;
	int last = palindrome.length() - 1;

	bool test = (isPalindrome(palindrome, first, last));
		cout << test;

}

bool isPalindrome(string palindrome, int forward, int backward)
{
	if (palindrome[forward] != palindrome[backward])
		return (false);
	if (forward >= palindrome.length() - 1)
		return (true);

	//this line is incorrect
	//isPalindrome(palindrome, ++forward, --backward);

	//you need to return the value!
	return(isPalindrome(palindrome, ++forward, --backward));
}

you promised to return a value but failed to do so.
undefined behaviour.
Topic archived. No new replies allowed.