6 Replies - 178272 Views - Last Post: 28 March 2006 - 10:28 PM Rate Topic: -----

#1 Quiksilver  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 12-March 05

Stack Pop/Push Program

Posted 24 March 2006 - 04:40 PM

hey,
i've been working on this program for a couple days now, while doing some research on classes. heres my code so far, its giving me about 10 erros.

Quote

15 C:\Documents and Settings\Andrew\My Documents\C++\Stack\stack.cpp ISO C++ forbids declaration of `push' with no type


Quote

34 C:\Documents and Settings\Andrew\My Documents\C++\Stack\stack.cpp prototype for `int STACK::push(int)' does not match any in class `STACK'


Quote

73 C:\Documents and Settings\Andrew\My Documents\C++\Stack\stack.cpp no matching function for call to `STACK::STACK(int&)'


heres the code:
it would be greatly appreciated if someone could point me in the right direction as to what im doing wrong. those three errors occur more than once so its oviously something im repetedly doing wrong. thanks!

#include<iostream.h>
#include<fstream.h>
#include"apstring.h"
#include"apvector.h"
#include"apstring.cpp"

class STACK
{
      private:
              int top, stack[20];
      public:
             STACK();
             push();
             pop(int x);
             int current ()
                 {
                 return top;
                 }
};

STACK::STACK()
{
    top=-1;
    for(int i=0;i<20;i++)
        {
            stack[i]=0;
        }
    
}

STACK::push (int x)
{ 
    top++;
    stack[top] = x;
}


int STACK::pop ()
{ 
    top--;
    int x = stack[top];
    return x;
}


//Main
int main () 
{
    int pushnum;
    apstring choice;
    STACK a;
    do{
        cout<<"Enter 'push', 'pop', or 'q': ";
        cin>>choice;
        
        if(choice == "q")
        {
            system("PAUSE");
        }
        
        if(choice == "pop")
        {
            STACK pop();
            cout<<"Current number is: "<<a.current();
        }
          
        if(choice == "push")
        {
            cout<<"\n\nEnter number to push: ";
            cin>>pushnum;
            STACK push(pushnum);
            cout<<"\n\nCurrent number is: "<<pushnum;
        }
          
        }while(choice != "q");

system("PAUSE");
return 1;
}


This post has been edited by Dark_Nexus: 29 March 2006 - 10:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stack Pop/Push Program

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Stack Pop/Push Program

Posted 24 March 2006 - 10:28 PM

Well, there are a few problems...
 push();
pop(int x);


should be
void push(int x);
int pop();


in the class defintion...note there are multiple changes, including the fact that pop should have no argument, while push should (at least accoriding to your member function definitions).
 STACK pop();
//and
 STACK push(pushnum);


should likely be
a.pop();
//and
a.push(pushnum);


unless you are trying to redeclare the object...I assume you mean to use the object you already created.
STACK::push (int x)
{
top++;
stack[top] = x;
}


should be
void STACK::push (int x)
{
top++;
stack[top] = x;
}


as by ISO standards, the compiler is looking for a specified return type. I'm not near a compiler, but that should get rid of at least some of the errors...please note that I have not tested this program for functionality...merely done a cursory review to error check the syntax.
Was This Post Helpful? 1
  • +
  • -

#3 topcat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 06

Re: Stack Pop/Push Program

Posted 26 March 2006 - 04:21 PM

May I reccomend placing main in the begining of your program after you prototypes. Aslo comments in your code make it easier to follow when someone is trying to debug it. Finally, I think that you need to add a return 0; statement at the end of main to terminate the program. I hope that helps.
Topcat
Was This Post Helpful? 0
  • +
  • -

#4 Quiksilver  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 12-March 05

Re: Stack Pop/Push Program

Posted 26 March 2006 - 04:23 PM

thanks alot people. i got it up and running perfect. :)
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Stack Pop/Push Program

Posted 26 March 2006 - 06:17 PM

topcat, on 26 Mar, 2006 - 05:13 PM, said:

Finally, I think that you need to add a return 0; statement at the end of main to terminate the program.

Actually, the main function requires the return of an integer, not specifically zero...the choice of integer is up to the user. Having said that, the retun values are generally used to inidcate to calling processes the status upon exit of the program, and generally, zero is used to indicate success, with non zero numbers being used to indicate varying unsuccessful terminations.

I would be remiss, however, if I did not point out that calling the pause.exe with the system command is platform dependant.
Was This Post Helpful? 0
  • +
  • -

#6 Quiksilver  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 12-March 05

Re: Stack Pop/Push Program

Posted 28 March 2006 - 09:17 PM

okay, well im trying to add a destructor now, right below the third constructor.

STACK::~STACK () {
  delete top;
  delete stack;
}



i get this error:

Quote

53 - type `int' argument given to `delete', expected pointer


i've tried messing around with it and i cant seem to get around it.
can anyone help me out?

thanks

This post has been edited by Dark_Nexus: 29 March 2006 - 10:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Stack Pop/Push Program

Posted 28 March 2006 - 10:28 PM

The delete operator is meant to be used to delete pointers allocated by the use of the new keyword.
Was This Post Helpful? 1

Page 1 of 1