Question

Okay, so what I am trying to do is take an equation and put each character onto a stack in order to check for errors. Whenever I try to do this I get weird results, almost like it is just adding onto what was previously there but placing it in the next element.

Header:

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

// Stack template
template <class T>
class Stack
{
private:
    T *stackArray;
    int stackSize;
    int top;

public:
    //Constructor
    Stack(int);

    // Copy constructor
    Stack(const Stack&);

    // Destructor
    ~Stack();

    // Stack operations
    void push(T);
    void pop(T &);
    bool isFull();
    bool isEmpty();
};

//***************************************************
//  Constructor                                     *
//***************************************************

template <class T>
Stack<T>::Stack(int size)
{
    stackArray = new T[size];
    stackSize = size;
    top = -1;
}

//***************************************************
//  Copy constructor                                *
//***************************************************

template <class T>
Stack<T>::Stack(const Stack &obj)
{
    // Create the stack array.
    if (obj.stackSize > 0)
        stackArray = new T[obj.stackSize];
    else
        stackArray = NULL;

    // Copy the stackSize attribute.
    stackSize = obj.stackSize;

    // Copy the stack contents.
    for (int count = 0; count < stackSize; count++)
        stackArray[count] = obj.stackArray[count];

    // Set the top of the stack.
    top = obj.top;
}

//***************************************************
//  Destructor                                      *
//***************************************************

template <class T>
Stack<T>::~Stack()
{
    if (stackSize > 0)
        delete[] stackArray;
}

//*************************************************************
// Member function push pushes the argument onto              *
// the stack.                                                 *
//*************************************************************

template <class T>
void Stack<T>::push(T item)
{
    if (isFull())
    {
        cout << "The stack is full.\n";
    }
    else
    {
        top++;
        stackArray[top] = item;
    }
}

//*************************************************************
// Member function pop pops the value at the top              *
// of the stack off, and copies it into the variable          *
// passed as an argument.                                     *
//*************************************************************

template <class T>
void Stack<T>::pop(T &item)
{
    if (isEmpty())
    {
        cout << "The stack is empty.\n";
    }
    else
    {
        item = stackArray[top];
        top--;
    }
}

//*************************************************************
// Member function isFull returns true if the stack           *
// is full, or false otherwise.                               *
//*************************************************************

template <class T>
bool Stack<T>::isFull()
{
    bool status;

    if (top == stackSize - 1)
        status = true;
    else
        status = false;

    return status;
}

//*************************************************************
// Member function isEmpty returns true if the stack          *
// is empty, or false otherwise.                              *
//*************************************************************

template <class T>
bool Stack<T>::isEmpty()
{
    bool status;

    if (top == -1)
        status = true;
    else
        status = false;

    return status;
}
#endif

Driver:

// This program demonstrates the Stack template.
#include <iostream>
#include <string>
#include "Stack.h"
using namespace std;

// Constants for the menu choices
const int PUSH_CHOICE = 1,
POP_CHOICE = 2,
QUIT_CHOICE = 3;

// Function prototypes
void menu(int &);
void getStackSize(int &);
void pushItem(Stack<string>&);
void popItem(Stack<string>&);

int main()
{
    int stackSize; // The stack size
    string equ;
    //int choice;    // To hold a menu choice

    // Get the stack size.
    //getStackSize(stackSize);

    cout << "Enter the equation: ";
    cin >> equ;

    // Create the stack.
    Stack<string> stack(equ.length());

    for(int i=0;i<equ.length(); i++){

        stack.push(equ.substr(i, i+1));

    }

    for(int i=0; i<equ.length(); i++){

        popItem(stack);

    }


    /*do
    {
        // Get the user's menu choice.
        menu(choice);

        // Perform the user's choice.
        if (choice != QUIT_CHOICE)
        {
            switch (choice)
            {
            case PUSH_CHOICE:
                pushItem(stack);
                break;
            case POP_CHOICE:
                popItem(stack);
            }
        }
    } while (choice != QUIT_CHOICE);
    */


    system("Pause");

    return 0;
}

//************************************************
// The getStackSize function gets the desired    *
// stack size, which is assigned to the          * 
// reference parameter.                          *
//************************************************
void getStackSize(int &size)
{
    // Get the desired stack size.
    cout << "How big should I make the stack? ";
    cin >> size;

    // Validate the size.
    while (size < 1)
    {
        cout << "Enter 1 or greater: ";
        cin >> size;
    }
}

//************************************************
// The menu function displays the menu and gets  *
// the user's choice, which is assigned to the   *
// reference parameter.                          *
//************************************************
void menu(int &choice)
{
    // Display the menu and get the user's choice.
    cout << "\nWhat do you want to do?\n"
        << PUSH_CHOICE
        << " - Push an item onto the stack\n"
        << POP_CHOICE
        << " - Pop an item off the stack\n"
        << QUIT_CHOICE
        << " - Quit the program\n"
        << "Enter your choice: ";
    cin >> choice;

    // Validate the choice
    while (choice < PUSH_CHOICE || choice > QUIT_CHOICE)
    {
        cout << "Enter a valid choice: ";
        cin >> choice;
    }
}

//************************************************
// The pushItem function gets an item from the   *
// user and pushes it onto the stack.            *
//************************************************    
void pushItem(Stack<string> &stack)
{
    string item;

    // Get an item to push onto the stack.
    cin.ignore();
    cout << "\nEnter an equation: ";
    //cin >> item;
    getline(cin, item);
    stack.push(item);
}

//***************************************************
// The popItem function pops an item from the stack *
//***************************************************
void popItem(Stack<string> &stack)
{
    string item = "";

    // Pop the item.
    stack.pop(item);

    // Display the item.
    if (item != "")
        cout << item << " was popped.\n";
}
Was it helpful?

Solution

std::string::substr takes an index and a length, not two indices. You're taking longer and longer substrings. Just use 1 as the second parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top