stack::push and stack::pop are wrong way to execute methods (not mentrioning missing parameters) in your program.
If the stack is defined as pointer to dynamic object then you write stack->pop(); and when it is static stack.pop(); .
You also didnt initialize the stack. Class stack that you created is just a definition of object (type). Same as int, charor floatyou must create it: stack myStack;
then you can execure methods: mystack.push();
etc.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
usingnamespace std;
/*----------------------------------------------------------------------------------------------------*/
class stack
{
struct stackListNode // Struct linked list
{
char data;
stackListNode *next;
};
stackListNode *top;
public:
void push(char);
int pop();
int peek();
int IsEmpty();
stack() // Creates stack, and sets top to NULL
{
top = 0;
}
};
/*----------------------------------------- Push ------------------------------------------------------*/
void stack::push(char symbol) // Push symbol onto the stack and create new node for top of stack
{
stackListNode *newNode = new stackListNode;
newNode-> data = symbol;
newNode-> next = top;
top = newNode;
}
/*--------------------------------------- Pop ---------------------------------------------------------*/
int stack::pop()
{
if(IsEmpty())
{
cout << "Stack is empty!\n" << endl;
return (2);
}
stackListNode *temp = top;
top = temp-> next;
}
/*------------------------------------------ Peek --------------------------------------------------------*/
int stack::peek()
{
if(IsEmpty())
{
cout << "Stack is empty!\n" << endl;
return (2);
}
return top-> data;
}
/*--------------------------------------------- Is Empty --------------------------------------------------*/
int stack::IsEmpty()
{
return top == NULL;
}
/*--------------------------------------------- Main ------------------------------------------------------*/
int main(int argc, char *argv[])
{
ifstream input_File;
input_File.open ("stack.cpp"); // "stack.cpp"
if(!input_File.is_open())
{
cout << "Error: Unable to open input file\n" << endl;
return(1);
}
/*-------------------------------------------- Push Switch/Case-----------------------------------------*/
stack myStack;
char symbol;
char commentSlash = '/';
char commentStar = '*';
if(input_File.is_open())
{
while(!input_File.eof())
{
input_File.get(symbol);
if(symbol == ' ') // If character is 'space', continue
continue;
else
{
switch(symbol)
{
case'(': myStack.push(symbol); // Push ( onto stack
cout << "Top of Stack: " << myStack.peek() << endl << endl;
case'{': myStack.push(symbol); // Push { onto stack
cout << "Top of Stack: " << myStack.peek() << endl << endl;
case'[': myStack.push(symbol); // Push [ onto stack
cout << "Top of Stack: " << myStack.peek() << endl << endl;
case'/': // Read /, continue, read next: if = *,
input_File.get(symbol); // then symbol value = */, push symbol
if(symbol == '*')
{
symbol = commentSlash; // Push /
myStack.push(symbol);
cout << "Top of Stack: " << myStack.peek() << endl << endl;
symbol = commentStar; // Push *
myStack.push(symbol);
cout << "Top of Stack: " << myStack.peek() << endl << endl;
}
/*------------------------------------------ Pop Switch/Case -------------------------------------------*/
case'*':
input_File.get(symbol);
if(symbol == '/')
{
myStack.pop(); // Pop *
myStack.pop(); // Pop /
}
case')':
if(myStack.peek() == '(')
{
myStack.pop(); // Pop match ( )
}
else
{
cout << "Error! Symbol not balanced: " << symbol << endl;
return (1);
}
case'}':
if(myStack.peek() == '{')
{
myStack.pop(); // Pop match { }
}
else
{
cout << "Error! Symbol not balanced: " << symbol << endl;
return (1);
}
case']':
if(myStack.peek() == '[')
{
myStack.pop(); // Pop match [ ]
}
else
{
cout<< "Error! Symbol not balanced: " << symbol << endl;
return (1);
}
break;
} // end switch
} // end else
} // end while
}// end if
cout << "No Errors Found." << endl << endl;
return 0; // Success
}// end main
------------------------------------------------------------------------------------------------------------------
I believe I am not pushing the symbols onto the stack correctly, as well as popping...
Any suggestions?