The following program will read in a character string, copy it to a character array, enqueue and push the characters into a stack, and then dequeue and pop the stack to compare the letters. The point is to determine whether or not the word is a palindrome.
The code debugs just fine and brings up the command prompt like it should. However, when I enter a word, the command prompt proceeds past the comparison and asks for another word to enter without displaying whether or not the word is a palindrome.
The code is below, and the class files are at the bottom. Can someone tell me what's wrong?
Code:
#include <iostream>#include <string>
#include "queue.h"
#include "stack.h"
using namespace std;
int main()
{
string z;
int y = 0;
char x[20];
stack a;
queue b;
cout << "Please enter a word below to test whether or not it is a palindrome." << endl;
cout << "This program will both push the characters into a stack and enqueue the" << endl << "characters, and then pop/dequeue respectively to compare letters." << endl << endl;
cout << "If you would like to quit at any time, enter 'Q'." << endl << endl;
cout <<"Please enter a word to be tested: ";
cin >> z;
x[0] = NULL;
while (x[0] != 'Q')
{
y = 0;
while (x[y] != NULL)
{
x[y] = z[y];
a.stack::push(x[y]);
b.queue::enqueue(x[y]);
x[y] = NULL;
y++;
}
while (!a.isEmpty())
{
while (!b.isEmpty())
{
char c, d;
while (y >= 0)
{
a.pop(c);
b.dequeue(d);
if (c != d)
{
cout << "That word is not a palindrome." << endl << endl;
goto exit;
}
else if (y == 0 && c == d)
cout << "That word is a palindrome." << endl << endl;
else
y--;
}
}
}
exit:
cout << "If you would like to run the function again, enter a word to be tested." << endl;
cout << "Otherwise, enter 'Q' to quit." << endl << endl;
cout << "Please enter a command: ";
cin >> x;
}
system("pause");
return 0;
}
stack::stack(void)
{
top = NULL;
}
stack::~stack(void)
{
}
queue::queue(void)
{
front = NULL;
}
queue::~queue(void)
{
}
void stack::push(char x)
{
Node *temp;
temp = new Node; // create a new node to hold the item to be pushed
temp->item = x; // store the item
temp->next = top; // link the node into the list
top = temp; // adjust the top pointer
}
void stack::pop(char &x)
{
if (!isEmpty())
{
x = top->item; // send the top item by reference
Node *temp;
temp = top; // set a temporary pointer to top
top = top->next; // move the top pointer to new top position
delete temp; // delete the node that was popped
}
}
void queue::enqueue(char x)
{
Node *temp;
temp = new Node; // create a new node to hold the item to be pushed
temp->item = x; // store the item
if (front == NULL)
front = temp;
else
temp->next = back; // link the node into the list
back = temp; // adjust the back pointer
}
void queue::dequeue(char &x)
{
if (!isEmpty())
{
x = front->item; // send the front item by reference
Node *temp;
temp = front; // set a temporary pointer to front
front = front->next; // move the front pointer to new front position
delete temp; // delete the node that was dequeued
}
}
bool stack::isEmpty()
{
return (top == NULL);
}
bool queue::isEmpty()
{
return (front == NULL);
}
#pragma once
class stack
{
private:
struct Node
{
char item;
Node *next;
};
Node *top;
public:
// Constructor & Destructor
stack(void);
~stack(void);
// Member functions
void push(char x);
void pop(char &x);
bool isEmpty();
};
#pragma once
class queue
{
private:
struct Node
{
char item;
Node *next;
};
Node *front;
Node *back;
public:
// Constructor and Destructor
queue(void);
~queue(void);
// Member functions
void enqueue(char x);
void dequeue(char &x);
bool isEmpty();
};