Here is just some things you might want to think about that aren't really
part of any code in specific. Assuming that the stack is set up correctly
this should only actually take two for loops and a comparion with a cout
statement to run if I understand the problem correctly. You are just
supposed to take in a word from the user and then. Basically say "Yes this
is a palindrome" or "No this isn't a palindrome." I'm assuming this is for
some class you are taking right now or else from some book that your
reading. Either way. It was hard for me to follow your code and understand
what you are trying to do.
Basically this is the approach I would take.
First of all the class
The clear_stack function looks fine.
The emtpy_stack function should not have to take an argument. Top is
already a variable that is a public part of the class although it shouldn't
be public. Nor should the array be. You don't need any external data to
figure it out.
Full stack you lack the data and have to look else where. But this
shouldn't be true. You should have a "max" or "capacity" variable in your
class which is mazimum number of elements that it can contain. Then your
function would look fine with no arguments being passed.
Push looks okay, but it depends on how you are using your array index.
Remember with an array of size 8 the index run from 0 to 7 not from 1 to 8.
Your pop function looks ok as well, however I would have just returned a
char with it as opposed to using a reference variable inside. But that is
personal preference.
Set up your array as dynamic memory. It and befor you do a push funtion
check to see if the stack is full and if it is allocate more memory for it.
I am assuming you guys covered a little bit of it. You might not have I
know we did similar things earlier in my c++ courses too where we just hard
coded the size in.
With that in mind look at your main program and think about what you are
doing. I don't know exactly what your code is trying to do but if I read it
correctly it isn't doing it right.
I am assuming you know what a palindrome is although I had to look it up
myself just to make sure
Its a word like bob or dad where it is the same
backwards and forwards. So now the question is how to use a stack to check
for them. Well you started off with the right idea you need to take the
user input of a word again, I don't think you want to go one at a time here.
Take in a char*. I am assuming you guys covered something of pointers and
such. If not then I guess you have to keep doing what you are doing. But a
char* would be a lot easier and require less loops and everything else.
Loop one should be a for loop where you look at each letter of the word and
input it into the stack.
should look something like this.
for(int i=0; i<strlen(word);i++)
s.push(word
);
}
Now, the neat thing about a stack is when you take them off they will be in
reverse order.
so that should be the second loop;
char* new_word = new char[strlen(word)+1]
int k = 0;
while(!s.empty_stack())
{
s.pop(new_word[k]);
i++;
}
I'm sure there is a better way of writing the above function in a for loop.
But I couldn't think of one without a size function in the palindrome_stack
class. Which wouldn't be a bad addition.
now all you need yet is a simple if statement
if(strcmp(word, new_word)==true)
cout << word << " is a palindrome.";
else
cout << word << " is not a palindrome.";
and that should be all you have to write. i hope this helps a bit.
~Justin
Will said:
Sorry guys,
here is the actual code...
------------------------------------
#include <iostream.h>
#include <fstream.h>
#include <string>
class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);
int array[9];
int top;
};
int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;
char newarray[8];
s.clear_stack();
cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";
i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
cin >> test;
Here you should add an initial "cin >> word;" so that the first time your
run through the push function you aren't just throwing random garbage onto
your stack.
Also, I think you probably understand this but just from glancing at your
code. Are you sure you want word to be just a char? I would have made it a
char* and then accessed each character individually and added it to the
stack. The way it is set up now the user can only enter one character at a
time and then hit enter and run through the word that way. you should
change your cout above to indicate if that is what you want them to do.
while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}
// for (int z=1; z<9; z++)
// {
// cout <<"\n new array "<<newarray[z];
// }
cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;
for (z=1; z<=9; z++)
{
cout << word;
cin >> newarray[z];
}
}
cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;
while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}
return 0;
}
// CLEARING THE STACK
void palindrome_stack::clear_stack()
{
top = 0;
}
// DETERMINING IF THE STACK IS FULL
bool palindrome_stack::full_stack(int max)
{
if (top == max)
return true;
else
return false;
}
//PUSHING TO THE STACK
void palindrome_stack:
ush(char word)
{
top = top + 1;
array[top] = word;
}
// CHECK TO SEE IF STACK IS EMPTY
bool palindrome_stack::empty_stack(int top)
you don't need the argument above.
top is already a part of the function.
if (top == 0)
return true;
else
return false;
}
// POPPING FROM THE STACK
void palindrome_stack:
op(char &word)
{
word = array[top];
top = top - 1;
}
Will said:
Hi,
Sorry to be a pest...
But I can figure this out.
I'm pushing to a stack. then I need to check to see if the word is a
palindrome.
Is the code below correct?
if so, how can I check the entire word?
when I display "word" I get the last letter pushed into the stack.
Any help would be greatly appreciated.
thank you,
Bill
----------------------code ------------------
#include <iostream.h>
#include <fstream.h>
#include <string>
class palindrome_stack
{
public:
void clear_stack();
bool empty_stack(int top);
bool full_stack(int max);
void push(char word);
void pop(char& word);
int array[9];
int top;
};
int main()
{
palindrome_stack s;
int i, max;
char word;
max = 8;
std::string test;
char newarray[8];
s.clear_stack();
cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";
i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
while (!(s.full_stack(max)))
{
s.push(word);
i++;
cin >> word;
cout <<"\n"<<word;
}
cout <<"\n TOP "<<s.top;
while (s.top != 0)
{
s.pop(word);
cout << word;
for (z=1; z<=9; z++)
{
cout << word;
cin >> newarray[z];
}
}
cout <<"\n array value "<<s.array[s.top];
cout <<"\n word value "<<word;
cout <<"\n top value "<<s.top;
cout <<"\n i value "<<i<<endl;
while (!(s.empty_stack(s.top)))
{
s.pop(word);
cout << word <<" ";
}