1. Turn OFF TABS in your text editor. It might look OK for you, but when you come to post code to the board, it's a unholy mess. Try pressing the review button to make sure your post is presentable.
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void header();
int main()
{
int front;
bool done = false;
bool palindrome = true;
string::size_type back;
string input;
header();
cout << "Enter your word or sentence that is less than 20 characters." << endl;
cout << "Let your sentences only consist of letters and spaces." << endl;
cout << "When finished, press 0." << endl << endl;
getline(cin, input);
while (input[0] != '0') {
front = 0;
back = input.length() - 1;
palindrome = true;
while (done == false) {
input[front] = tolower(input[front]);
input[back] = tolower(input[back]);
string input; // GET RID OF THIS!!!!
if (input[front] == input[back]) {
front++;
back--;
} else {
palindrome = false;
done = true;
}
if (front >= back) {
done = true;
}
}
if (palindrome == true) {
cout << "\"" << input << "\"" << " is a palindrome." << endl;
} else {
cout << "\"" << input << "\"" << " is not a palindrome." << endl;
}
done = false;
getline(cin, input);
}
cout << "Ending program..." << endl;
return 0;
}
void header()
{
cout << "************************" << endl;
cout << " Palindrome Detection " << endl;
cout << "************************" << endl << endl;
}
Well you created header();, so why not create some more functions.
Then your main loop might look like this
Code:
while (input[0] != '0') {
palindrome = checkForPalindrome(input);
if (palindrome == true) {
cout << "\"" << input << "\"" << " is a palindrome." << endl;
} else {
cout << "\"" << input << "\"" << " is not a palindrome." << endl;
}
getline(cin, input);
}
Functions mean you can test things without having the rest of the code there
Code:
int main ( ) {
bool result1 = checkForPalindrome("hello");
bool result2 = checkForPalindrome("abba");
}
See, a simple main and your function, and that's it. Now how easy would that be to test?
See the comment in the code?
Well what you've done here is create a shadow for your input. Your actual input is now invisible to the rest of the code in this block, and the shadow input is just some uninitialised variable which you then try and do if (input[front] == input[back])
> Apparently, the getline command has some bugs in the actual library file
So, if your compiler is VC++ 6, then do as it says.
Actually, getting a better C++ compiler would be a better idea - VC++6 sucks at being a C++ compiler.