can someone please tell me why this is giving me a segmentation fault? I am sure it has something to do with the first line. Thanks!!

int main()
{
  StackType<char> temp;

  char sym;
  cin >> sym;
  while (sym != '#')
   {
    if (sym == '{' || sym == '(')
     {
      temp.push(sym);
     }
    else if (sym == ')' || sym == '}')
     {
      temp.pop();
     }
   cin >> sym;
   }

  cout << endl;

My crystal ball tells me you're calling pop() before you've called push(). It could also be due to a mismatch.

For more detailed analysis, provide more details, such as how the stack works or an input string that causes the error.

well an input string would be{(35+4)*6}. I am trying to see if the equation is balanced or not. I am trying to read in the first symbol- and if its a bracket or parentheses- i want it to be pushed into the stack. If its an operator or number it should be ignored. Then if it reads in the other end of the bracket or parentheses it should pop the other one out- as to "cancel" them. At the end i want it to have if the stack is empty- then the equation is balanced. If there is still something in the stack- it is not balanced.

And are you sure your input is correct? If you would provide something like : {(35+4)*16)} it would definitly cause a segmentation error.

Edited 6 Years Ago by Nick Evan: n/a

Nope my input {(35+4)*6} is correct. I want it to read in the first bracket- and since its in the "if" criteria- i want it to be pushed into the stack. Then it will read in the next symbol which is the (. Again it fits the "if" criteria so it should push it. Then it reads in the 3 and should be discarded, 5 discard, + discard, 4 discard and then the ). Now it hits the second if statement and i want (i guess the first ( to be popped out). Ok i see where i went wrong- can you point me in the right direction? Anyways it continues from there.

This is what i have now---- still off on the pop thing:

int main()
{
  StackType<int> L1;

  int temp;
  char sym;
  cin >> sym;
  while (sym != '#')
   {
    if (sym == '{' || sym == '(')
     {
      L1.push(sym);
     }
    else
      cin.ignore();
    if (sym == ')' || sym == '}')
     {
      L1.pop();
     }
    cin >> sym;
   }
  if (L1.isemptystack())
   cout << "The expression has only matching grouping symbols" << endl;
  else
   cout << "The expression has some unmatched grouping symbols" << endl;

thanks

if (sym == ')' || sym == '}')

That looks for either ')' or '}'. You need to look for exact matches. I.e. if '(' is on the stack it is only correct if it finds the matching ')' immediately next...

So when its reading through my input and the first thing it reads is a '{' then it will go into my stack. The next thing it reads is the '(' and that will go in my stack. SO then is it skipping everything else until it comes to the ')'??? I want it to read that one- and then since the top item in the stack is '(', it should remove it since it found its "partner". I know what i want it to do- but is that feasible? If so- am i on the right track? Thanks

Read my post again. Then, stop and write your solution on paper. Draw a flow chart even. When that is done then move onto the coding.

Here is a crude attempt -- feel free to modify and polish it.

#include <iostream>
#include <stack>
#include <map>
#include <string>
#include <iterator>

using namespace std;

int main()
{
    stack<char> list;
    map<char, char> symbols;
    map<char, char>::iterator it;
    const string VALID = "(){}";
    symbols['('] = ')';
    symbols['{'] = '}';

    char symbol = 0;

    while(symbol != '#')
    {
        cin >> symbol;
        getchar();

        if(VALID.find(symbol) != string::npos)
        {
            if(!list.empty() && ((symbols[list.top()] == symbol)))
            {
                list.pop();
            }
            else
            {
                list.push(symbol);
            }
        }
    }

    if(list.size() == 0)
        cout << "Its a well formed expression";
    else
    {
        cout << "Its not a well formed expression";
        cout << "The excess characters are: " << endl;
        while( !list.empty() )
        {
            cout << list.top() << '\n';
            list.pop();
        }
    }
    getchar();
}

Thank you very much for your help. I can see where you are going on this....but some of the things had me a little confused. I am still a new programmer- but i totally see your idea. Thanks very much for your help!

This article has been dead for over six months. Start a new discussion instead.