Everything below works the first time through, and the validation of the user's choice to proceed or not works, as does his choice to quit; but when it gets to cin.getline it just blows right through it without waiting for input. As my parents used to say, Where have I gone wrong?
//CIS 180 Rich Mansfield 0457321 11/20/09
//This program checks to see if an input is a palindrome
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
const int MAX = 81;//Array size; no magic numbers; last char is null terminator
char str[MAX];//Array to hold raw user input
char proceed;//To allow user to choose to proceed or not, after initial run
do //one time initially; user gets to choose to repeat or not at end
{
// Get a line of input.
cout << "Input a string of no more than "
<< (MAX - 1) << " characters:\n";//saves last space for null terminator
cin.getline(str, MAX);//getline allows white space chars
//Doesn't pick up getline on second iteration
// Display the input one character at a time.
int count = 0;// Loop counter variable
cout << "The sentence you entered is:\n";
while (str[count] != '\0')
{
cout << str[count];
count++;
} //End while going through string
int strlength;//var used to find pairs of characters to compare
strlength = strlen(str);
cout << endl << endl;
//Convert all the characters in the string str to uppercase
for(int i = 0; i < MAX; i++)
if(str[i])//if there IS a character at that location
str[i] = toupper(str[i]);//make sure it's uppercase (if already upper, no change)
cout << str << endl << endl;
//Remove all the special characters except letters a - z
char str2[MAX];//Will need a new string to hold just the alpha dogs
int j=0;//and a counter for its elements
//str2 is declared to max out at 81, but I don't have to look at all of it
for(int i = 0; i < strlength; i++)//strlength is all I need to look at
if(isalpha(str[i]))//if the next character is a letter,
{
str2[j] = str[i];//pass it into the new string
j++;//increment j to go to next element in new string
} //End of if isalpha
str2[j]= '\0';//Put null terminator at end of new string
cout << str2 << endl
<< "strlength is " << strlength << endl
<< "j is " << j
<< endl << endl;
int back = j - 1;//back index points to the last character, drops the \0
int flag = 1;//start with true value for flag; set to break if it goes false
//Compare the first character with the last character.
//If they're are the same, compare the next characters.
//front index is pointed to the first character in the string str2
for(int front = 0; front <= j/2 ; front++)//j is the position of the \0 in str2
//so j/2 is the midpoint that both back and front are converging on;
//as long as front < j/2, it keeps incrementing.
{
if(str2[front] != str2[back - front])//comparing first element (0)
//to last element (j - 1, or back, is last element, not counting \0
//subtracting front el doesn't change that, at first, because 1st position is 0
//but from then on, back decrements as fast as front increments
{
flag = 0;
break;
} //End if string pairs not equal
} //End for loop comparing all pairs of characters
if(flag == 0)
cout << "It is not a palindrome" <<endl;
else
cout<<"It's a palindrome"<<endl;
//Re-initialize basic array in case user wants to do it again
//Nope; doesn't work whether I have the next two lines, or not
const int MAX = 81;//Array size; no magic numbers; last char is null terminator
char str[MAX];//Array to hold raw user input
//Does the user want to do it again? pg495
cout << "One more time? (y/n) ";
cin >> proceed;
//Validate response
while (tolower (proceed) != 'y' && tolower(proceed) != 'n')
{
cout << "Please enter y or n: ";
cin >> proceed;
}
} while (tolower(proceed) == 'y');//End do while
return 0;
} //End main fn