Thread: Help Please "array stack"

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Help Please "array stack"

    This is my first time on this forum. I have to make this program check for balanced parentheses. I have most of the work done. But i keep getting "cannot convert `int*' to `STACK*' for argument `1' to `void push(STACK*, int)' ". I marked where errors are in program. I believe i am not implementing the functions right please any help would be greatly appreciated. I know its probably something dumb and small.

    Thanks alot in advance for any help

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    char* filename;
    #define MAX 300
    
    
    
    typedef struct stack
    {
    int item[MAX];
    int Top;
    }STACK;
     void push(STACK *ps, int x)
        {
            if (ps->Top == MAX) {
                fputs("Error: stack overflow\n", stderr);
                abort();
            } else
                ps->item[ps->Top++] = x;
        }
            int pop(STACK *ps)
        {
            if (ps->Top == 0){
                fputs("Error: stack underflow\n", stderr);
                abort();
            } else
                return ps->item[--ps->Top];
        }
    
    int main ()
    {
        
       
        size_t found;
        string str, tmp;
        cout <<"Please enter the name of the file" << endl;
    	filename=new char;
    	cin >> filename;
    	ifstream myfile(filename);
    	while(!myfile.eof())
    	{
        getline(myfile, tmp);
    	str += tmp;
        }
      
      
    
     int array[300];
    
      found=str.find_first_of("(");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of("(",found+1);
       push(array, 1);              //  error 
      }
    found=str.find_first_of(")");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of(")",found+1);
       pop(array, 1);              //  error 
      }
        found=str.find_first_of("{");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of("{",found+1);
         push(array, 1);            //  error 
      }
    found=str.find_first_of("}");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of("}",found+1);
       pop(array, 1);            //  error 
      } 
        found=str.find_first_of("[");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of("[",found+1);
        push(array, 1);             //  error 
      }
    found=str.find_first_of("]");
      while (found!=string::npos)
      {
        str[found]='*';
        found=str.find_first_of("]",found+1);
       pop(array, 1);              //  error 
      }
      
    
    
    
    
      return 0;
    }
    Last edited by seanman13579; 03-22-2011 at 12:00 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where are you declaring your stack?

    It looks like your 'int array[300]' should be 'STACK stack' and then you can pass the address of that stack variable to your push() and pop() functions (i.e. push(&stack, 1)).
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Hey itsme86 thanks man i know i was doing something wrong that was small and dumb. lol
    Last edited by seanman13579; 03-22-2011 at 12:25 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    char* filename;
    
    ...
    
    int main ()
    {
        ...
    
        cout <<"Please enter the name of the file" << endl;
        filename=new char;
        cin >> filename;
        ifstream myfile(filename);
    If you are truly set on using dynamic memory allocation to store this, then I think you want to consider that a user's likely going to need more than just a single character for the file name. A single char can only hold a string of length zero (the null character itself). You should also be calling delete for everything that you new. However, I would just recommend using a std::string to store the file name instead.



    #2
    Code:
    while(!myfile.eof())
    {
        getline(myfile, tmp);
        ...
    }
    Avoid using end-of-file tests to control your loops. Instead, test the result of the read operation itself:
    Code:
    while(getline(myfile, tmp))
    {
       ...
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    One last question I want to have it display whether its balanced or not
    Code:
     if (&stack = 0)  // i know this is not right but how would i get the value out.
     { 
                cout << "your program is balanced" << endl;
                }
       else
       {
             cout << "your program is not balanced" << endl;
    
    }
    Also thanks hk_mp5kpdw for the help changes made.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by seanman13579 View Post
    One last question I want to have it display whether its balanced or not
    Code:
     if (&stack = 0)  // i know this is not right but how would i get the value out.
     { 
                cout << "your program is balanced" << endl;
                }
       else
       {
             cout << "your program is not balanced" << endl;
    
    }
    if(stack.Top == 0) // Balanced
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Ok i hate to keep asking question but im now getting a new error. When i run it on a windows based complier it just crashes and when i run it on a UNIX complier i get a segmentation fault (core dumb) error. Any ideas ?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, you're not initializing Top anywhere. Maybe:
    Code:
    Stack stack;
    stack.Top = 0;
    Personally, I'd write the stack code as a class with the push() and pop() functions as part of the stack so the stack stuff is encapsulated.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    One again thank you itsme86 so much works fine now. Will make suggested changes too thanks so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I have an "Array of Strings"?
    By abh!shek in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2008, 01:35 PM
  2. Replies: 1
    Last Post: 12-08-2007, 10:49 PM
  3. "array does not point to an object type"
    By Welshy in forum C Programming
    Replies: 2
    Last Post: 02-27-2006, 02:06 PM
  4. Essentially an "array of pointers" problem
    By Matt13 in forum C Programming
    Replies: 4
    Last Post: 03-12-2004, 03:36 AM