Palindrome Detector Problem
Apr 6, 2016 at 3:11am UTC
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 Apr 6, 2016 at 3:36am UTC
Apr 6, 2016 at 6:46am UTC
You're using the assignment operator here, which makes
isPalin
false, thus the loop will never run.
You need the equality operator.
Though your program isn't working as expected.
test
This is a palindrome!
Last edited on Apr 6, 2016 at 6:47am UTC
Apr 6, 2016 at 2:31pm UTC
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" );
}
Apr 6, 2016 at 2:57pm UTC
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.