Debug Assertion Fail

I'm am unsure where this problem is. It says line 1440, but i do not know where that is and what the issue is.

#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;

do
{

cout << "Enter a sentence to check: " << endl;
getline(cin, sentence);


numletters = sentence.length();

for (i=0; i<numletters; i++)
{
letter = tolower(sentence[i]);

if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
lower_sentence = lower_sentence + letter;
}
}

compressed = lower_sentence;


for (i=0; i<=numletters; i++)
{
numletters = compressed.length();
letter = tolower(sentence[i]);

if (letter != ' ' && letter != '.' && letter != ',' && letter != '!' && letter != '?')
{
reversed = letter + reversed;
}
}


cout << "Compressed: " << compressed << endl;
cout << "Reversed: " << reversed << endl;

if (reversed == lower_sentence)
{
cout << "===>Palindrome!!!" << endl;
}
else
cout << "===>Not Palindrome." << endl;


cout << "----------------------------------------------------------------------------" << endl;
cout << "Do you want to enter another? [Y or N]" << endl;
cin>> choice;
cout << "----------------------------------------------------------------------------" << endl;
} while (choice == "Y" || choice == "y");

system ("pause");
return 0;
}
Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/

The problem is this:
1
2
3
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");

system ("pause");
return 0;
}
If I remove the "=" from: for (i=0; i<numletters; i++)
No you didn't (the scond loop): for (i=0; i<=numletters; i++) // still <=

And you still don't use code tags

why don't you use lower_sentence in the second loop? You wouldn't need if and tolower again
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");

system ("pause");
return 0;
}
Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/


You have two problems:

1. cin>> choice; leaves the new line in the stream. The next getline() will get an empty string.

Solution: use either ignore() or getline()

2. lower_sentence and reversed are using the values from the last iteration. They get longer and longer

Solution: either clear both or move all variables (except choice) at the top of your do loop
Last edited on
Topic archived. No new replies allowed.