br007
br007

Reputation: 11

Stack Simulation In C++

I wrote this program to simulate a stack. Line 87 doesn't function as expected. Can anyone point out the problem that is occurring ? And is line 94 the correct way to increment the pointer's address by 1 i.e to the next element in the array ? Note that the lines are pointed out in the comment. It is in the module entitled "void stack::push(void)". Thanks in advance.

The problem is in this module:

void stack::push(void) {
 clrscr();
 string input;
 bool check=false;
 if (data[0]=="") {
     tos=data;
 } else if (data[size-1]!="") { // This block doesn't function as expected.
     cout << "Stack is full. Press Enter to return to main menu.";
     check=true;
     getch();
 } else { 
     tos++; // Is this the way to increment a pointer's address ?? 
 }
 if (check==false) {
     cout << "Enter data to store in stack: ";
     cin >> input;
     *tos=input;
 }
 clrscr();
}

The full program code is as follows:

 // Stack Simulation
 #include <iostream>
 #include <conio>
 #include <string>
 using namespace std;

 class stack {
     private:
     string *tos; // Top of stack pointer  
     string *data;
     int size;
     public:
     stack(void);
     ~stack(void);
     void menu(void);
     void push(void);
     void pop(void);
     void display(void);
 };

 stack::stack(void) {
     cout << "Stack Simulation\nPress Enter To Continue.";
     getch();
     clrscr();
     int size;
     cout << "Enter size of stack: ";
     cin >> size;
     data=new string[size];
     for (int k=0;k<size;k++) { // Nullifying every element of stack
         data[k]="";
     }
     clrscr();
 }

 stack::~stack(void) {
     clrscr();
     cout << "Press Enter to exit simulation.\n";
     getch();
 }

 void stack::menu(void) {
     int x;
     do {
         cout << "1.Push\n2.Pop\n3.Display\n4.Exit\nEnter your choice:";
         cin >> x; 
         switch (x) {
             case 1:
             push();
             break;
             case 2:
             pop();
             break;
             case 3:
             display();
             break;
             default:
             cout << "Invalid input. Inputs must be one of these numbers: 1,2,3. Try Again.\n";
             break;
         }
     } while (x!=4); 
 }

 void stack::display(void) {
     clrscr();
     if (data[0]=="") {
         cout << "Stack is empty. Press Enter to return to main menu.";
         getch();
     } else {
         cout << "Contents of stack:\n\n";
         for (int k=size-1;k>-1;k--) {
             if (!(data[k]=="")) {
                 cout << data[k] << endl;
             }
         }
         cout << "\n\nPress Enter to return to main screen.";
         getch();
     }
     clrscr();
 }

 void stack::push(void) {
     clrscr();
     string input;
     bool check=false;
     if (data[0]=="") {
         tos=data;
     } else if (data[size-1]!="") { // This block doesn't function as expected.
         cout << "Stack is full. Press Enter to return to main menu.";
         check=true;
         getch();
     } else { 
         tos++; // Is this the way to increment a pointer's address ?? 
     }
     if (check==false) {
         cout << "Enter data to store in stack: ";
         cin >> input;
         *tos=input;
     }
     clrscr();
 }

 void stack::pop(void) {
     clrscr();
     if (data[0]!="") {
         cout << "The data popped is " << *tos;
         *tos="";
         tos--;
         cout << "\nPress Enter to return to main menu.";
         getch();
     } else {
         cout << "Stack is empty.\nPress Enter to return to main menu.";
         getch();
     }
     clrscr();
 }

 void main(void) {
     stack demo;
     demo.menu();
 }   

Upvotes: 0

Views: 1181

Answers (2)

M. Yousfi
M. Yousfi

Reputation: 594

Line 87 doesn't work as expected as you said, probably because you are defining a local variable int size in the Stack's constructor, and it's probably not what you wanted (you probably wanted to set the size data member).

However the previous answer is probably the correct answer for you question : your class is doing too much things. The role of the constructor is definitely not to clear the screen and to ask the user for some data to input.

Upvotes: 0

aslg
aslg

Reputation: 1974

  1. Line 87 doesn't function as expected. Can anyone point out the problem that is occurring ?

Not really, because you didn't say what you expect your function to do and because your stack is more than a stack. I'll address that in a moment.

  1. And is line 94 the correct way to increment the pointer's address by 1 i.e to the next element in the array ?

Yes.


First, a stack is a container of objects. Period.

When you create a stack you do this,

stack::stack(void) {
    cout << "Stack Simulation\nPress Enter To Continue.";
    getch();
    clrscr();
    int size;
    cout << "Enter size of stack: ";
    cin >> size;
    data=new string[size];
    for (int k=0;k<size;k++) { // Nullifying every element of stack
        data[k]="";
    }
    clrscr();
}

You make an introduction to your program, you clear the screen after the user presses any key, then request the user to enter the size for your stack and then initialize the stack. Do you get it now? You're writting a program inside the constructor of a container, in the wrong place.

Before actually writing code we should think of how to separate concerns in our programs. You want to make a stack, then make an interface for it.

A stack will contain objects in a way that you can only push, pop or get an object on the top of the stack. You can add other functions such as checking if the stack is empty, or how many elements it holds but those are the 3 basic functions.

Your constructor should look more like this,

stack::stack( int size ) :
    _maxSize( size ), _currentSize( 0 )
{
    data = new string[size];
}

The constructor lost some weight. Now it does something rather simple, it initializes the stack with a certain size provided to it from outside. There's no need to initialize every element to the null string, you will initialize your elements when the user pushes something onto the stack.

Now you have to put those extras in their rightful place, which could look like,

int main( int argc, char **argv ) {
    cout << "StackSimulation\nPress Enter To Continue." << endl;
    getch();
    clrscr();

    int size;
    cout << "Enter size of stack: ";
    cin >> size;
    stack myStack{ size };

    // ...
}

Now I don't want to write your code for you. You do it.

One of the goals of having code in well-defined modules is reducing the chance for errors. If something is wrong with the code you wrote for the stack, then you will find the error in the limited scope of code written for the stack, not in the code for menus or the code for input.

Currently it's hard to understand if the errors you're getting are due to something wrong with the stack itself or the code that has nothing to do with stacks.

Upvotes: 1

Related Questions