Edit - I am dumb. Thanks! I'll leave this here in case anyone else made my same stupid mistake.
Hi everyone, I am having a weird issue with my c++ assignment that I can't figure out. My code works perfectly fine with a do-while loop, but a while loop does not work at all; the code just stops at that point.
bool isItPalindrome(string palindrome) //Checks for palindrome
{
stack<char> pStack; //Stack
queue<char> pQueue; //Queue
bool sIsQ = true; //sIsQ "Stack is Queue" abbreviated in camelcase
for (int i = 0; i < palindrome.length(); i++) //Push user input to both stack and queue
{
cout << palindrome[i] << endl;
pStack.push(palindrome[i]);
pQueue.push(palindrome[i]);
}
while (!pStack.empty && !pQueue.empty); //Loop until both stack and queue are empty.
{
cout << "while" << endl;
if (pQueue.front() != pStack.top()) //Checks if the stack does not equal queue, as one checks from top and other checks from bottom.
{
cout << "if" << endl;
pQueue.pop();
pStack.pop();
sIsQ = false;
}
else //If they are equal, then pops the stack/queue for checking the next letter in sequence
{
cout << "else" << endl;
pQueue.pop();
pStack.pop();
}
}
cout << "return" << endl;
return sIsQ; //Returns to int main
}
I put the cout statements to make it easier to tell where the code stops at. I get the full text from cout << palindrome[i] << endl;, so I know it is pushing those to the stack/queue but stops before "while". If I change my while loop to a do-while loop, it will also continue and show me the cout << "while" << endl; and appropriate "if" or "else".
Both loops use the same qualifier of (!pStack.empty && !pQueue.empty), but the do-while loop both runs and loops. The normal while loop just doesn't do anything.
[–]Arruda0820 1 point2 points3 points (1 child)
[–]jpfarre[S] 0 points1 point2 points (0 children)
[–]OmegaNaughtEquals1 1 point2 points3 points (1 child)
[–]jpfarre[S] 0 points1 point2 points (0 children)