Hello,
What I am trying to do is create the classic palindrome detection program. I am having quite some trouble to get it working properly. The problems I am having are:
1) Whenever a string evaluates as a palindrome, every string after that is never a palindrome.
2) In my posted code, I am using cin >> input instead of getline(cin, input) because using the getline gives me weird results for my program. If anyone has any ideas why, that'd be great.
Here is the brains of the operation:
Code:
cin >> input;
while (input[0] != '0') {
if (input[20]) {
cout << "Your sentence/phrase can only be 20 characters long." << endl;
cout << "Try again..." << endl;
}
back = input.length() - 1;
while (done == false) {
if (input[front] == input[back]) {
front++;
back--;
cout << "Testing 1" << endl;
} else {
palindrome = false;
done = true;
cout << "Testing 2" << endl;
}
if (front >= back) {
done = true;
cout << "Testing 3" << endl;
}
}
if (palindrome == true) {
cout << input << " is a palindrome." << endl;
} else {
cout << input << " is not a palindrome." << endl;
}
palindrome = true;
done = false;
cin >> input;
}
cout << "Ending program..." << endl;
return 0;
}
As you can tell, I have inserted some cout testing statements as I was trying to pinpoint my problem.
As I progess, I need to discard whitespaces and accept capital letters, but as of right now, I am just trying to get the darn thing to work. I appreciate any ideas. Thank you.