I am writing a program to prove if a code is a Palindrome or not and must use stacks. I have written a code below that compiles, but when I run it, it outputs the word in an a stack view, but does not follow the rest of my program or does nothing else. Basically I think it is "popping" it but for some reason not "pushing" back in. Also, I don't think it is excluding spaces and case insensitive. Can anyone help?
Here is the code:
Thanks!

*****************************************************

#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
class stack_type // Declaration of Class
{
public:
 
void clear_stack(); 
bool empty_stack(); 
bool full_stack(); 
void push(char word); 
void pop(char& word); 
int stack [200];
int top;
}; 
int main ()
{
stack_type s;
int i;
char word;
std::string test;
char phrase[200], new_phrase[200]; 
char response; 
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>> word;
cout<<word<<endl; 
 
while (!(s.full_stack())&& word!='quit')
{
s.push(word); 
phrase[i]=word; 
i++; 
cin>>word; 
cout<<word<<"\n"; 
}
//end while
 
int j = 0;
while(!(s.empty_stack()))
{
s.pop(word); 
new_phrase[j]=word;
j++;
}
//end while
 
for(int k=0; k<j; k++)
cout << phrase[k]; 
//end for
 
for (k=0; k<j; k++)
{
if (phrase[k]==new_phrase[k])
cout << word << " This word is a palindrome. \n";
else 
cout << word << " This word is not a palindrome. \n";
}
cout << endl;
cout << "\nMore (Y or N)? ";
cin >> response;
 
while (response == 'Y' || response == 'y');
return 0;
}
//----------------------------------------------------------------------
void stack_type::clear_stack()
{
top = 0;
}
//----------------------------------------------------------------------
bool stack_type::empty_stack()
{
if (top==0)
return true;
else
return false;
}
//----------------------------------------------------------------------
bool stack_type::full_stack()
{
if (top==200)
return true;
else
return false;
}
//----------------------------------------------------------------------
void stack_type::push(char word)
{
top = top + 1;
stack[top]=word;
}
//----------------------------------------------------------------------
void stack_type::pop(char& word)
{
word = stack[top];
top = top - 1;
}

Recommended Answers

All 8 Replies

> char word;
Just a thought, perhaps the program might work better if this were a string instead of a single character? ;)

you mean like char [200]? or is there a different data type you are thinking of?

you mean like char [200]? or is there a different data type you are thinking of?

Sorry! My bad; I misread your code (I didn't see your loop below).

Arrgh. I keep reading your code way too fast. Anyway, here are some problems with your code:

Your input method is messed up. You should have a char[200], and then input each char from the string into the stack one-by-one. Your program hangs because cin runs out of characters to fill word with, and then it just sits there waiting for more input. You can also trash that while (!(s.full_stack())&& word!='q') statement, because I honestly don't see why you would want to enter a 200-letter palindrome.

Ok, I am closer...

//Keri DeGross
// CS 501 - Professor Pinto
// Project 2 Part 1: Determining Palindromes
#include <iostream>
#include <fstream>
using namespace std;
class stack_type // Declaration of Class
{
public:
void clear_stack(); 
bool empty_stack(); 
bool full_stack(); 
void push(char word); 
void pop(char& word); 
int stack [200];
int top;
}; 
int main ()
{
stack_type s;
int i;
char word[200];
char phrase[200], new_phrase[200]; 
char response; 
s.clear_stack();
cout <<"\nDisplay the top, after clearing the stack --> "<<s.top<<"\n";
i=0;
int z=0;
cout <<"\nEnter a word or phrase to see if it is a Palindrome!\n";
cin>> word[200];
cout<<word[200]<<endl; 

for (i=0; i<z; i++)
{
s.push(word[i]); 
phrase[i]=word[i]; 
}
int j=0; 
while(!(s.empty_stack()))
{
s.pop(word[i]); 
new_phrase[j]=word[i];
j++;
}
//end while
for(int k=0; k<j; k++)
cout << phrase[k]; 
//end for
for (k=0; k<j; k++)
{
if (phrase[k]==new_phrase[k])
cout << word << " This word is a palindrome. \n";
else 
cout << word << " This word is not a palindrome. \n";
}
cout << endl;
cout << "\nMore (Y or N)? ";
cin >> response;
while (response == 'Y' || response == 'y');
return 0;
}
//----------------------------------------------------------------------
void stack_type::clear_stack()
{
top = 0;
}
//----------------------------------------------------------------------
bool stack_type::empty_stack()
{
if (top==0)
return true;
else
return false;
}
//----------------------------------------------------------------------
bool stack_type::full_stack()
{
if (top==200)
return true;
else
return false;
}
//----------------------------------------------------------------------
void stack_type::push(char word)
{
top = top + 1;
stack[top]=word;
}
//----------------------------------------------------------------------
void stack_type::pop(char& word)
{
word = stack[top];
top = top - 1;
}

Whatever code-coloring method you're using, please stop it. Instead use code tags which are immensly better, and have coloring built-in if you use

[code=cplusplus]    cin>> word[200];
    cout<<word[200]<<endl;

Do you perhaps realize what you're doing?

When you reference it with word[200] , it basically means the 200th char (or letter in this case). So in reality you're only inputting one character.

The correct way to do it would be something like this:

cin>>word;
cout<<word<<endl;

Whatever code-coloring method you're using, please stop it. Instead use code tags which are immensly better, and have coloring built-in if you use (code=language)

And Format your code!!!!! Slamming everything to the left edge makes the code impossible to read!

commented: You tell 'em - there's way to much slop on DW - Salem +6

Thanks for the help. I am going to work on it after work again tonight. I don't know where the color code is coming from, it is not like that on my Visual. Same with the formatting, it just pasted that way. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.