Jun 8, 2012 at 8:08am UTC
#include<iostream>
using namespace std;
#define size 10
class stack
{
private:
int num;
int array[size];
int top;
public:
stack();
~stack();
void push(int data);
void pop(int &);
bool empty();
bool full();
};
stack::stack()
{
top=-1;
}
stack::~stack() {}
void stack::push(int data)
{
if(!full())
{
top++;
data=array[top] ;
}
else
cout<<"The stack is full!!"<<endl;
}
void stack::pop(int & data)
{
if(!empty())
{
top--;
array[top]=num;
cout<<num<<" ";
}
else
cout<<"The stack is empty!!"<<endl;
}
bool stack::empty()
{
if(top==-1)
return true;
else
return false;
}
bool stack::full()
{
if(top==size-1)
return true;
else
return false;
}
void main()
{
stack sample;
int num=99;
int data;
while(num!=0)
{
cout<<"Enter a number OR 0 to stop: ";
cin>>num;
sample.push(num);
}
cout<<"The data in reverse order is: ";
while(!sample.empty())
{
sample.pop(num);
}
}
Last edited on Jun 8, 2012 at 8:15am UTC
Jun 8, 2012 at 8:31am UTC
To help you out, here is your code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include<iostream>
using namespace std;
#define size 10
class stack {
private :
int num;
int array[size];
int top;
public :
stack();
~stack();
void push(int data);
void pop(int &);
bool empty();
bool full();
};
stack::stack() {
top=-1;
}
stack::~stack() {}
void stack::push(int data) {
if (!full()) {
top++;
data=array[top] ;
} else
cout<<"The stack is full!!" <<endl;
}
void stack::pop(int & data) {
if (!empty()) {
top--;
array[top]=num;
cout<<num<<" " ;
} else
cout<<"The stack is empty!!" <<endl;
}
bool stack::empty() {
if (top==-1)
return true ;
else
return false ;
}
bool stack::full() {
if (top==size-1)
return true ;
else
return false ;
}
int main() {
stack sample;
int num=99;
int data;
while (num!=0) {
cout<<"Enter a number OR 0 to stop: " ;
cin>>num;
sample.push(num);
}
cout<<"The data in reverse order is: " ;
while (!sample.empty()) {
sample.pop(num);
}
return 0;
}
And these are the warnings
Code Blocks Compiler wrote:C:\...\main.cpp|36|warning: unused parameter 'data'|
C:\...\main.cpp||In function 'int main()':|
C:\...\main.cpp|65|warning: unused variable 'data'|
||=== Build finished: 0 errors, 2 warnings ===|
Does that help?
Last edited on Jun 8, 2012 at 8:33am UTC
Jun 8, 2012 at 8:39am UTC
Also, in your stack, what does num do? I don't see if being used anywhere in your class. I believe you also have you assignment operators mixed up, for example
data=array[top] ;
array[top]=num;
These should be switched.
I also don't think you want to add the 0 the user enters to the stack
1 2 3 4 5 6
while (num!=0) {
cout<<"Enter a number OR 0 to stop: " ;
cin>>num;
sample.push(num);
}
Should be
1 2 3 4 5 6 7
while (num!=0) {
cout<<"Enter a number OR 0 to stop: " ;
cin>>num;
if (num != 0)
sample.push(num);
}
Last edited on Jun 8, 2012 at 8:45am UTC
Jun 8, 2012 at 8:52am UTC
although i change everything that u all suggested/ corrected ..but the pop output is still keep repeat the barbage(not the output tat i expected)!!
Jun 8, 2012 at 8:59am UTC
You didn't understand what Volatile Pulse wrote.
Remove line 8.
Line 31/39 makes no sense.