| | |
How to write a program using a stack
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Nuez_Jr
Consider the definition of a stack - last-in, first-out. If you load a bunch of stuff into a stack, and then take it all out again, you now have your original items *in reverse order*.
Thank you so much for your response. I understand the logic but somehow it's not reading that it's the reverse when I enter the two strings like this for example....hello:olleh. It should say "These two strings ARE the reverse of each other but instead it's saying that they are not. Can you please look over this code and see what I've done wrong. I know it's something with my code in main. If you could please reply as soon as possible I would really appreciate your help. Again Thanks.........
#include<iostream>
using namespace std;
const char MAX_ITEMS = 50;
class Stack
{
char mElements[MAX_ITEMS];
char mLetter;
int mTop;
public:
Stack();
bool IsEmpty()const;
bool IsFull();
bool Push(char letter);
bool Pop(char &letter);
void MakeEmpty();
};
int main()
{
Stack myStack;
char letter;
char other_letter;
char ans;
bool different = false;
do
{
cout<< "Enter two strings separated only by a : ";
cin.get(letter);
while(letter!= ':' && letter!= '\n' && !myStack.IsFull())
{
myStack.Push(letter);
cin.get(letter);
}
if (letter != ':')
cin.get(letter);
different = true;
while (!myStack.IsEmpty() && letter!= '\n')
{
myStack.Pop(other_letter);
if (other_letter != letter)
different = true;
cin>>letter;
}
if (!myStack.IsEmpty() && letter =='\n')
different = true;
if(myStack.IsEmpty() && letter != '\n')
{
while (letter!= '\n')
cin.get(letter);
different = true;
}
if (!different )
{
cout<< "The two strings ARE the reverse of each other. ";
cout<<endl;
}
else
{
cout<< "The two strings ARE NOT the reverse of each other. ";
cout<<endl;
}
cout<< "Do you want to enter another set?(y/n) ";
cin>>ans;
cin.get(letter);
myStack.MakeEmpty();
}
while (ans!= 'n' && ans!= 'N');
return 0;
}
Stack:
tack(){
mTop = -1;
}
void Stack::MakeEmpty()
{
mTop = -1;
}
bool Stack::IsEmpty()const
{
return(mTop == -1);
}
bool Stack::IsFull()
{
return(mTop == MAX_ITEMS -1);
}
bool Stack:
ush(char letter){
if(!IsFull())
{
mTop++;
mElements[mTop] = letter;
return true;
}
else
return false;
}
bool Stack:
op(char &letter){
if(!IsEmpty())
{
mLetter=mElements[mTop];
mTop--;
return true;
}
else
return false;
}
![]() |
Similar Threads
- how to write a program for power (C)
- How can I write this program. (C++)
- How can I write a program with 3 classes? (C)
- Trying to write a program that you can enter #s and multiply at the end. (Java)
- write a program that simulates rooling a pair of dice. (Java)
Other Threads in the C++ Forum
- Previous Thread: Help Writing Fuction to Read Fractions From User
- Next Thread: Program for calculating miles per gallon
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





