Hello All!
I'm a beginner at this C++ thing, and I'm kinda at a loss. I'm doing this school project to to write a program that will read a string entered by the user, and tell the user if said string is a palindrome. A palindrome is a word that could be read backwards and still be the same thing. For example, 'Bob' is the same backwards and forwards.
Well, for the most part, I've got it to work. Just two problems:
Problem 1. I keep getting this warning: "Debug Assertion Failed - string subscript out of range". I don't even know where to begin to fix that one!
Problem 2. I have an outer loop so that the user can enter as many string as they want to see if they're palindromes, but for some reason, the program takes that input and tries to determine if THAT is a palindrome. For example, the user should enter a '0' to continue the program, and if entered, the program tries to determine if '0' is a palindrome!
I'm sure that is a lot to soak in, and for that, I apologize. But with my limited programming skill-set, I feel like I'm pounding my head against a brick wall. Oh, please help this wretched soul!
Thank you in advance!
The code for the program is below:
# include <iostream>
# include <string>
using namespace std;
//Initialization of arrays
const int MAX = 100;
char array1[MAX] = {};
char array2[MAX] = {};
int main ()
{
//Initializaion of variables
string line = "";
bool done = false;
while(!done)
{
//Prompt user for input
cout<<"Please enter a string to find if it is a palindrome: ";
getline(cin, line);
//Copies the line without any punctuation into the first array
int count = 0;
for(int i = 0; i<length; i++)
{
if(isalpha(line[i]))
{
array1[count] = line[i];
cout<<array1[i];
}
count++;
//This is to ensure that no elements are skipped by the elements that would hold non-letter characters
if(!isalpha(line[i]))
{
count--;
}
}
cout<<endl;
//Copy values to the second array - backwards
int x = 0;
for(int i = length; i >= 0; i--)
{
if(isalpha(line[i]))
{
array2[x] = line[i];
//**********************************************************************
//This is just to assist with trouble shooting
cout<<array2[x];
cout<<"The i value is "<<i<<" ";
cout<<"The x value is "<<x<<" ";
//**********************************************************************
}
x++;
//This is to ensure that no elements are skipped by the elements that would hold non-letter characters
if(!isalpha(line[i]))
{
x--;
}
cout<<endl;
}
cout<<endl;
//Compare the two arrays
bool equal = true;
for(int i = 0; i<length; i++)
{
cout<<array1[i]<<" = "<<array2[i]<<endl;
if(array1[i] != array2[i])
{
equal = false;
}
}
//**********************************************************************
//This will print if the user's input is a palindrome or not
if(equal)
{
cout<<line<<" is a palindrome."<<endl;
}
else
{
cout<<line<<" is not a palindrome."<<endl;
}
//**********************************************************************
//Prompt user for more testing
cout<<"More palindrome testing? Enter 0 to continue or 1 to stop."<<endl;
cin>>done;
}