for (i=0; i<=numletters; i++) // '<=' means out of bounds. remove the '='
{
numletters = compressed.length(); // this is odd. move it before the for loop
If I remove the "=" from: for (i=0; i<numletters; i++) and move numletters=compressed.length();
I still receive the debug assertion fail and the first letter of my reversed is no longer there.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Declarations: Declaring variables
string sentence, choice, reversed, lower_sentence, compressed;
int numletters, i;
char letter;
// Main loop is Do While loop
do
{
// Get User's info
cout << "Enter a sentence to check: " << endl;
getline(cin, sentence);
numletters = sentence.length();
// Making all letters lower case and removing (. , ? !) from sentence
for (i=0; i<numletters; i++)
{
letter = tolower(sentence[i]);
if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
lower_sentence = lower_sentence + letter;
}
}
compressed = lower_sentence;
numletters = compressed.length();
// Reversing each letter to write sentence backwards
for (i=0; i<=numletters; i++)
{
letter = tolower(sentence[i]);
if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
reversed = letter + reversed;
}
}
// Displaying what the compressed and reversed versions look like
cout << "Compressed: " << compressed << endl;
cout << "Reversed: " << reversed << endl;
// If Else statement to show if the sentence is a palindrome
if (reversed == lower_sentence)
{
cout << "===>Palindrome!!!" << endl;
}
else
cout << "===>Not Palindrome." << endl;
// Asking User if the want to enter another sentence to check
cout << "----------------------------------------------------------------------------" << endl;
cout << "Do you want to enter another? [Y or N]" << endl;
cin>> choice;
cout << "----------------------------------------------------------------------------" << endl;
// While the user enters Y or y for yes, loop continues
} while (choice == "Y" || choice == "y");
sorry I hit undo before reposting. I have the debug fail gone, but could you please help with my loop placement. If the user types "y" or "Y" the loop uses the same input as before.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// Declarations: Declaring variables
string sentence, choice, reversed, lower_sentence, compressed;
int numletters, i;
char letter;
// Main loop is Do While loop
do
{
// Get User's info
cout << "Enter a sentence to check: " << endl;
getline(cin, sentence);
numletters = sentence.length();
// Making all letters lower case and removing (. , ? !) from sentence
for (i=0; i<numletters; i++)
{
letter = tolower(sentence[i]);
if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
lower_sentence = lower_sentence + letter;
}
}
compressed = lower_sentence;
// Reversing each letter to write sentence backwards
for (i=0; i<numletters; i++)
{
numletters = compressed.length();
letter = lower_sentence[i];
if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
reversed = letter + reversed;
}
}
// Displaying what the compressed and reversed versions look like
cout << "Compressed: " << compressed << endl;
cout << "Reversed: " << reversed << endl;
// If Else statement to show if the sentence is a palindrome
if (reversed == lower_sentence)
{
cout << "===>Palindrome!!!" << endl;
}
else
cout << "===>Not Palindrome." << endl;
// Asking User if the want to enter another sentence to check
cout << "----------------------------------------------------------------------------" << endl;
cout << "Do you want to enter another? [Y or N]" << endl;
cin>> choice;
cout << "----------------------------------------------------------------------------" << endl;
// While the user enters Y or y for yes, loop continues
} while (choice == "Y" || choice == "y");