Palindrome Detector Problem

So ive attempted at creating a palindrome detector but everytime I run it I enter the palindrome test string and the console pauses having seemingly run through all the logic. Why will nothing appear?
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
44
45
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string testPalin;
	bool isPalin = false;
	
	getline(cin, testPalin);

	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::isspace), testPalin.end());
	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::ispunct), testPalin.end());
	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::isdigit), testPalin.end());

	if (testPalin.length() % 2 != 0)
	{
		testPalin.erase(testPalin[testPalin.length() % 2 + 1]);
	}

	int i = 0;
	int o = testPalin.length() - 1;

	while (isPalin = false)
	{
		if (testPalin[i] != testPalin[o])
		{
			cout << "Not a Palindrome" << endl;
			return 0;
		}
			
		if (testPalin[i] = testPalin[o -1])
		{
			isPalin = true;
			cout << "This is a palindrome!" << endl;
		}

		i++;
		o--;
	}
	
	system("PAUSE");
	
}
Last edited on
 
while (isPalin = false)

You're using the assignment operator here, which makes isPalin false, thus the loop will never run.

You need the equality operator.
 
while (isPalin == false)


Though your program isn't working as expected.

test
This is a palindrome!
Last edited on
Alright so I changed a few things (including what you pointed out first integralfx, thank you very much for that) but I now I get an out of range exception im pretty sure my indexing on the string is fine though

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
44
45
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string testPalin;
	bool isPalin = false;
	
	getline(cin, testPalin);

	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::isspace), testPalin.end());
	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::ispunct), testPalin.end());
	testPalin.erase(remove_if(testPalin.begin(), testPalin.end(), ::isdigit), testPalin.end());

	if (testPalin.length() % 2 != 0)
	{
		testPalin.erase(testPalin[testPalin.length() / 2 + 1]);
	}

	int i = 0;
	int o = testPalin.length() - 1;

	while (isPalin == false)
	{
		if (testPalin[i] != testPalin[o])
		{
			cout << "Not a Palindrome" << endl;
			return 0;
		}
			
		if (testPalin[i] == testPalin[o -1])
		{
			isPalin = true;
			cout << "This is a palindrome!" << endl;
		}

		i++;
		o--;
	}
	
	system("PAUSE");
	
}

For example, with the input word "beans"
testPalin.erase(testPalin[testPalin.length() / 2 + 1])
which is
testPalin.erase(testPalin[5 / 2 + 1])
which is
testPalin.erase(testPalin[3])
which is
testPalin.erase('n')

See something wrong with that last one?
Topic archived. No new replies allowed.