I know that you should quit when something works but you don't learn much that way. I substituted all the cin statements in the recursion with variable = cin.get()
The program then did the following:
If num was >1 then the screen scrolled forever after the string was entered. If num = 1, then the first example did'nt output the letter but every example worked after that.
What is the difference between cin and cin.get?
What is not being initialized or being initialized with the cin.get?
Code follows:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::boolalpha;
bool palindrome (int, int);
int main()
{
const int StopVal = 0;
int num;
cout << "Enter the number of letters in your candidate palindrome, or\n"
<< "enter " << StopVal << " to end this program.\n\n";
cout << "Enter the number of letters (" << StopVal << " to stop) here ==> ";
cin >> num;
while (num != StopVal) {
cout << "Enter the palindrome candidate ==> ";
if(palindrome(num/2, num%2))
cout << " is a palindrome.\n" << endl;
else
cout << " is not a palindrome.\n" << endl;
cout << "Enter number of letters for the next candidate ("
<< StopVal << " to stop) ==> ";
cin >> num;
cin.get();
}
return 0;
}
bool palindrome ( int half , int mod )
{
char letter1, letter2, oddletter;
static bool status = true;
static bool oddgot = false;
if ( half == 0 ) { // Handles case with only one character
letter1 = cin.get();
cout << letter1;
status = true;
return status;
}
if ( half-- >= 1){
status=true;
oddgot=false;
letter1 = cin.get();
cout << letter1;
if (half>0)
palindrome ( half, mod );
if ( mod == 1 && oddgot==false) {
oddletter = cin.get();
cout << oddletter;
oddgot=true;
}
letter2 = cin.get();
cout << letter2;
if ( letter1 != letter2 )
status = false;
}
return status;
}