Hi, I've been trying to get this to work for the past two hours and can't figure out why this run-time error is happening.

The code is as follows:

#include<queue>
#include<stack>
#include<iostream>
usingnamespace std;
int main()
{
stack<char,deque<char,allocator<char> > > s;
queue<char,deque<char,allocator<char> > > q;
char ch;
int count = 0;
char ans;
bool status = true;
do
{
if (s.empty() && q.empty())
{
cout << "Enter a series of characters in a string to check if it is a palindrome."
<< "Enter '!' when you are at the end of the string. " << endl;
do
{
cout << "Please enter a character." << endl;
cin >> ch;
if (ch == '!')
break;
else 
{
s.push(ch);
q.push(ch);
count++;
}
} while (ch != '!');
}
else
{
q.pop();
s.pop();
}
for(int i = 0; i < count; i++)
{
if (s.top() != q.front() )
{
cout << "The top of the stack is: " << s.top() << endl;
cout << "The front of the queue is: " << q.front() << endl;
status = false;
s.pop();
q.pop();
}
else
{
cout << "The top of the stack is: " << s.top() << endl;
cout << "The front of the queue is: " << q.front() << endl;
s.pop();
q.pop();
} 
}
if (status == false)
cout << "The entered string is not a palindrome." << endl;
else
cout << "The entered string is a palindrome!" << endl;
cout << "Would you like to enter another string of characters?" << endl;
cin >> ans;
} while(ans == 'y');
}

When I run it as so this happens:

Enter a series of characters in a string to check if it is a palindrome.Enter '!
' when you are at the end of the string.
Please enter a character.
a
Please enter a character.
b
Please enter a character.
a
Please enter a character.
!
The top of the stack is: a
The front of the queue is: a
The top of the stack is: b
The front of the queue is: b
The top of the stack is: a
The front of the queue is: a
The entered string is a palindrome!
Would you like to enter another string of characters?
y
Enter a series of characters in a string to check if it is a palindrome.Enter '!
' when you are at the end of the string.
Please enter a character.
a
Please enter a character.
b
Please enter a character.
a
Please enter a character.
!
The top of the stack is: a
The front of the queue is: a
The top of the stack is: b
The front of the queue is: b
The top of the stack is: a
The front of the queue is: a


And then a run-time error pops up saying something about an assertion failure. I guess I just dont understand what that is or what is causing it? Thanks for any help.

Recommended Answers

All 4 Replies

Reset variable count when you're reusing s and q variables. I.e. do count = 0; as the first thing inside the outer most do-while loop.
Problem is because you haven't reset this variable on second attempt you're popping more elements than you pushed. So when you pop on an empty stack assertion fails.

Finally use code tags instead of coloring the code manually, let the machine do that job. :)

I didn't color the code manually, just a copy/paste from Visual. And I got the code to work last night after a couple more hours. I just assigned the size of the stack to count. Sorry about the indentation, apparently a copy/paste takes that out, and I didn't see that.

It really helps when you put cout messages on after every push and pop to see what the size of the current stack and queue is. Thats how I found my problem. I'll just attach what I came up with, I don't like how this is taking out my identations, and yes I'm using code tags.

> I didn't color the code manually, just a copy/paste from Visual.
He means to use the bbcode [code=cplusplus] [/code] and it will syntax highlight your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.