Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,625 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,056 people online right now. Registration is fast and FREE... Join Now!




Stack Pop/Push Program

 
Reply to this topicStart new topic

Stack Pop/Push Program, Using Classes

Quiksilver
post 24 Mar, 2006 - 03:40 PM
Post #1


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


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!

CODE

#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 Mar, 2006 - 09:13 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 24 Mar, 2006 - 09:28 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Well, there are a few problems...
CODE

push();
pop(int x);

should be
CODE

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).
CODE

STACK pop();
//and
STACK push(pushnum);

should likely be
CODE

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.
CODE

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

should be
CODE

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.
User is offlineProfile CardPM

Go to the top of the page

topcat
post 26 Mar, 2006 - 03:21 PM
Post #3


New D.I.C Head

*
Joined: 28 Feb, 2006
Posts: 5


My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Quiksilver
post 26 Mar, 2006 - 03:23 PM
Post #4


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


thanks alot people. i got it up and running perfect. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 26 Mar, 2006 - 05:17 PM
Post #5


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


QUOTE(topcat @ 26 Mar, 2006 - 05:13 PM)
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.
User is offlineProfile CardPM

Go to the top of the page

Quiksilver
post 28 Mar, 2006 - 08:17 PM
Post #6


New D.I.C Head

Group Icon
Joined: 12 Mar, 2005
Posts: 45



Dream Kudos: 25
My Contributions


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

CODE

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 Mar, 2006 - 09:13 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2006 - 09:28 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


The delete operator is meant to be used to delete pointers allocated by the use of the new keyword.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:25AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month