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.