Question

I am writing a simple bracket checker. Should be pretty easy. I had it working when it was all in one function, but I am required to also make something for stdin. So I thought it was best to make 2 functions. That being said I am getting an error on the checking if the stack is null on line 82. for whatever reason it is not allowing me to check if the top of my stack is null. I tried in a testing program to see if it was some sort of referencing error or if it was going out of scope by going into the other method. Its not. Should work fine because it is a global variable.

Thoughts on what I am doing wrong? All my interneting and knowledge points me to the idea that I am doing it correctly.

Below is all of the code. its compilable. if I need to clarify anything I would be more than happy to.

Thanks

#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <fstream>
#include <cstdlib>
using namespace std;

stack<char> BracketsCheck;
int linecounter = 0;
int FileNumber = 1;
int pos;
string str ="";
string filename;

int validate(string string)
{
        int size = str.size();
        for (int i = 0; i < str.size(); i++)
        {
            pos = i;
            if ((str[i] == '(' ) || (str[i] == '[') || (str[i] == '{'))
            {
                BracketsCheck.push(str[i]);
            }
            else if (str[i] == ')')
            {
                if (BracketsCheck.top() == '(')
                    BracketsCheck.pop();
                else
                {
                    cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open parenthesis" << endl;
                    return EXIT_FAILURE;
                }
            }
            else if (str[i] == ']')
            {
                if (BracketsCheck.top() == '[')
                    BracketsCheck.pop();
                else
                {
                    cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open squre bracket" << endl;
                    return EXIT_FAILURE;
                }
            }
            else if (str[i] == '}')
            {
                if (BracketsCheck.top() == '{')
                    BracketsCheck.pop();
                else
                {
                    cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open curly brace" << endl;
                    return EXIT_FAILURE;
                }
            }
        }
}

int main(int argc, char* argv[])
{
//  BracketsCheck.top() = 'h';

    if (argc == 1)
    {
        cin >> str;
        cout << "no arguments" << endl;
        validate (str);
        return 0;
    }
    else
    {
        while (argv[FileNumber] != NULL)
        {
            filename = argv[FileNumber];
            ifstream inFile(argv[FileNumber]);
            cout << argv[FileNumber]<<endl;
            while (getline(inFile, str))
            {
                validate(str);
                linecounter++;
            }
            if (BracketsCheck.top() !=  NULL)
            {
                cout << "got to null checker" << endl;
                cout << filename << ":" << linecounter << ":" << pos << "umatched closing brace" << endl;
                return EXIT_FAILURE;
            }
            FileNumber++;
        }
        return 0;
    }
}
Was it helpful?

Solution

Based on your response to my comments. If you are trying to check if the stack is not empty you should use !BracketsCheck.empty() also:

int validate(string string)

is probably not a good idea since you will hiding the string type.

top() will return a reference or const reference not a pointer and if your stack is empty you should not be calling top.

I would also discourage you from using:

using namespace std;

it is considered bad practice I realize typing std:: all the time can be annoying at first but you really do get used to it after a while.

Finally validate needs a return statement since it is supposed to return int and flowing off the end of function without a return in this case will invoke undefined behavior as per 6.6.3 The return statement paragraph 2 from the draft C++ standard.

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