I have to use stacks to check an input file for parentheses matches.
I have a skeleton...but I am having some problems
Do I have to use a class or struct to do this?
Here is what I have in main
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h> // for exit(..)
#include <stdio.h>
#include "stack.h"
int main()
{
char c;
int k;
ifstream f;
f.open( "input.txt" );
if( ! f ){
cout << "Error opening file. Quitting.\n";
exit(-1);
}
//initialize
stk_init();
c = getchar();
putchar(c);
// loop through file one char at a time
while (c!=EOF)
{
// process one character
// push (, pop if ),
// check stack empty if \n,
// ignore others
// if error, move to next line
if (c == '(')
{
stk_push(c);
}
else if (c == ')')
{
if (c !== '\n')
stk_pop(c);
else if (c == '(')
stk_push (c);
// pop stack, if empty have missing (
}
else if (c == '\n')
{
// check on missing )
// set up for next line
stk_init();
}
if (stk_error()) //skip rest of line
else // get next char
{
}
} // endwhile
return 0;
}
my specific question first is
how do I check for the top of the stack?
I know i need to do this for the ')' first but I'm not sure how. Do I need to declare a variable for the top of stack?
Thanks