By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

push and pop - stacks

P: n/a
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 <<" ";
}
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
Hi,

Sorry to be a pest...
You're not being 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?
No. See below.
if so, how can I check the entire word?

when I display "word" I get the last letter pushed into the stack.
You have provided insufficient information for
any meaningful diagnosis.

----------------------code ------------------
#include <iostream.h>
#include <fstream.h>


Putting aside for the moment these nonstandard
headers:

You have omitted the information critical to
determining the cause of the problem: the
definitions of the member functions of
your 'palindrom_stack' class.

Post the actual code for the program which is
not behaving as you desire. This missing pieces
prevent us from analyzing it or compiling it and
observing its behavior, so of course we have no
way of identifying where you went astray.

-Mike
Jul 19 '05 #2

P: n/a

"Will" <ke****@secret.com> wrote in message news:NC********************@comcast.com...
#include <iostream.h>
#include <fstream.h>
#include <string>
Do not USE iostream.h and fstream.h. They are not standard. Many implementations
actually get a lot of heartburn when you mix the standard versions of the files with the
bogus ones (as you do with std::string and non-std::iostream).
class palindrome_stack
Well you might show us the implementation of this.
int i, max;
char word;
Declare these as you need (and initialize them). The above is fraught with perils as you
see later.
std::string test;
You should not clutter your confused application with things you aren't using.

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);
The first time through the loop, word has no deterministic value at this point.
cin >> newarray[z];


What are you doing here? You read 9 characters from the standard input without
any prompt to the user. You also write leave newarray[0] unset, and write two
bytes past the end of the allocation. All this is very bad.
Jul 19 '05 #3

P: n/a
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;

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::push(char word)
{
top = top + 1;
array[top] = word;
}

// CHECK TO SEE IF STACK IS EMPTY
bool palindrome_stack::empty_stack(int top)
{
if (top == 0)
return true;
else
return false;
}

// POPPING FROM THE STACK
void palindrome_stack::pop(char &word)
{
word = array[top];
top = top - 1;
}



"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
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 <<" ";
}

Jul 19 '05 #4

P: n/a

"Will" <ke****@secret.com> wrote in message news:Wb********************@comcast.com...
Sorry guys,

here is the actual code...
You didn't fix the things we pointed out.

void palindrome_stack::clear_stack()
{
top = 0;
}
Would be nice if your class had a constructor that initialized it
to clear.

// DETERMINING IF THE STACK IS FULL
bool palindrome_stack::full_stack(int max)
Why do you make the caller pass in the stack size, when you
are not ready to really handle numbers bigger than 8. The
class should manage this.

if (top == max) \ void palindrome_stack::push(char word)
{
top = top + 1;
array[top] = word;
}
You will write off the end of your "array" arg. You have several
major problems. FIRST, you should let the caller decide if he
has pushed too many things on the stack, you should check it
here. You know you leave array[0] unset in this case. Probably
would be better to increment top AFTER storing word.
// POPPING FROM THE STACK
void palindrome_stack::pop(char &word)
{
word = array[top];
top = top - 1;


Again you shouldn't trust the caller to not pop an empty stack here.
Jul 19 '05 #5

P: n/a


Will wrote:

Sorry guys,

here is the actual code...
Suggestion:

Fire up your debugger and step through the code.
While doing do so examine the values of variables as they
change. This way you will get a better feeling of what is going
on in which order in your code.

eg.

[snip] i=0;
int z=0;
cout <<"\nEnter a word to see if it is a Palindrome!\n";
cin >> test;

while (!(s.full_stack(max)))
{
s.push(word);
Interesting. When the loop is entered the first time, 'word' has not
received any value up to now, yet you push it on the stack ...
i++;
cin >> word;
and now 'word' receives a value the first time ...
cout <<"\n"<<word;
}


.... but if the stack happens to be full, the last read character
in 'word' isn't processed at all.

As said: use your debugger to step through the code. It will be a good
exercise in techniques on how to find logical bugs in your code.
BTW: logical bugs and how to find and fix them is an important issue
in everday life as a programmer. So developing skills on how to do
this is an important issue too.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #6

P: n/a
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[i]);
}

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" <ke****@secret.com> wrote in message
news:Wb********************@comcast.com...
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::push(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::pop(char &word)
{
word = array[top];
top = top - 1;
}



"Will" <ke****@secret.com> wrote in message
news:NC********************@comcast.com...
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 <<" ";
}


Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.