I absolutely am stuck, I'm so close to finishing this, I've messed up somewhere along the way unfortunately. Any help would be appreciated if you'd be so kind. A second opinion never hurts!
I'm just a beginner so excuse any lack of knowledge. I may have made this ultimately harder than it needs to be, but it's how I was able to solve this puzzle.
I'm trying to get it to check an inputted string to see if when it's rearranged backwards, it matches, ignoring white space and non letter characters.
void stringtest()
{
string input, output;
cout<<"Enter a string: ";
cin.ignore(80, '\n');
getline(cin, input);
cout<<endl;
int length, totalL, lengthnew, runs;
lengthnew=0;
totalL=lengthnew-1;
runs=0;
length = input.length(); //total length
output=input; //used to make uppercase
for(int h=0; h<=length-1; h++)
{
output[h] = toupper(input[h]); //new string with all uppercase
}
string nospaces;
nospaces="";
for(int i=0; i<length; i++)
{
if(isalpha(output[i])) //testing to see if there is non letters
nospaces+=output[i]; //adds the letters to a new string
}
lengthnew = nospaces.length(); //total string length without whitespace or non letters
for(int i=0; i<=nospaces.length()-1; i++)
{
if(nospaces[i] == nospaces[totalL--]) //compares i (starts at beginning) to totalL-- (starts at end)
{
runs = runs++; //checks total times it's ran through, if runs successful it adds, and should equal length
}
}
if(runs == totalL)
cout<<"Yes the string is a palindrome.\n";
else
cout<<"No the string is not a palindrome.\n";
menu();
}
Line 13 means nothing. Remember, computers do things in order. You are asking the computer to do something with garbage data. (The computer won't "remember" this as an equation, because it is an assignment statement.)
Move the line down to line 34.
Line 43: Didn't you just modify the value of totalL? Why would it be equal to the length of nospaces? (Use nospaces.length().)
BTW, you have got way too many variables in there, which is contributing to your confusion. (Too many variables == trying to keep too many things in your mind at once == mistakes.)
Once you get the program working, try to reduce the number of variables you need. (Don't forget to backup first!)