-
Stack using push and pop
Hi everyone, I've been doing pretty well while learning C++. Unfortunaltely I ve been having major problems with Classes since this is very new to me. I've been trying for a few days now to write a program that will allow a user to continuoisly enter numbers (as long as it is less than 9) and have them pile up on each other using "push". I want the program to also pop all of the elements leaving only the high number (above 9). At first I tried to have the user pop the number themselves and then use peek to look at the stacks contents but became so frustrated that I just tried to get one part working successfully. Unfortunately this part is not working either. Can you pelase guide me in the right directions because I feel as if I am way off; I even went out to buy a C++ book by Deitel and Deitel but am still having a little trouble.
Any help would be greatly appreciated
Code:
#include <iostream.h>
const int Stack_Size=100;
/*The stack itself*/
class Stack{
public:
stack(void);/*initialize the stack*/
void push (const int item); /*push an item*/
int pop (void);
int peek (const int item);
bool empty (const int item); /*is stack empty?*/
bool full (const int item); /* Is stack full*/
private:
int count; /*number of items in the stack*/
int data [Stack_Size]; /*The items themselves*/
};
#include "Stack.h"
Stack::Stack()
{
count=0; /*initialize the top of the stack*/
void Stack::push(const int item)
{
count++;
data[count]=item;
}
int Stack::pop()
{
int topval;
topval=data[count];
count--;
return(topval);
}
int Stack::empty()
{
if (count==0)
return TRUE;
else
return FALSE;
}
int Stack::full()
{
if (count==Stack_Size)
return TRUE;
else
return FALSE;
}
int main()
{
Stack contents;
int number;
cout<<" Enter as many digits as you'd like per line"
<<" To stop this program please enter a number greater than 9";
while(1)
{
cout<<"enter a digit";
cin>>number;
if (number>9)
break;
if (contents.full())
cout<<"no more space";
break;
else
contents.push(number);
cout<<"The values that were popped from the stack are";
while (!contents.empty())
{
cout<<contents.pop()<<endl;
}
return 0;
}
-
if your stack size is Stack_size = 100, then I would expect to be able to put up to 100 items/elements in the stack. However, data[0] will never be used in your code, so the max number of items/elements that can be entered by the user will be 99, not 100.
Likewise if you let count grow to 100, then you will (hopefully) crash the program when count == 100 in the following line
data[count]=item;
To fix this use count - 1 in all the []'s rather than count
change the first while loop conditional to this:
while(number < 10 && count < 101)
Then when the loop stops figure out why and tell the user. That way you get rid of all the breaks and allow the user to add up to 100 values less than 9. You might want to add some further data verification to make the program more robust, but that can come later.
on first glance you appear to have a mismatch between number of right and left curly braces. The first while statement doesn't have a closing brace, I think. To many breaks to be sure though.
-
well first this:
Code:
stack(void);/*initialize the stack*/
Should be
Code:
Stack(void);/*initialize the stack*/ //must have big S
You will have to rewrite this:
Code:
Stack::Stack()
{
count=0; /*initialize the top of the stack*/
void Stack::push(const int item)
{
count++;
data[count]=item;
}
int Stack::pop()
{
int topval;
topval=data[count];
count--;
return(topval);
}
int Stack::empty()
{
if (count==0)
return TRUE;
else
return FALSE;
}
int Stack::full()
{
if (count==Stack_Size)
return TRUE;
else
return FALSE;
}
Functions shouldnt be declared inside the constructor: watch this code as an example:
Code:
class someclass
{
someclass();
void somefunction();
int somenumber;
};
someclass::someclass()
{
somenumber = 0;
}
void someclass::somefunction()
{
//some code here
}
Code:
while(1)
{
cout<<"enter a digit";
cin>>number;
if (number>9)
break;
if (contents.full())
cout<<"no more space";
break;
else
contents.push(number);
cout<<"The values that were popped from the stack are";
while (!contents.empty())
{
cout<<contents.pop()<<endl;
}
return 0;
}
well you should check use of { and }
you have a little too few here: I *think* this is what you meant:
Code:
while(1)
{
cout<<"enter a digit";
cin>>number;
if (number>9)
break;
if (contents.full())
{
cout<<"no more space";
break;
}
else
contents.push(number);
}
cout<<"The values that were popped from the stack are";
while (!contents.empty())
{
cout<<contents.pop()<<endl;
}
return 0;
}
Or something like that, but you had a little too few brackets. Also please post the errors you get (if you get any but you should). Also like it is in your code now you have declared main as a part of the constructor to the Stack class.
-
Stack-Almost there
Hi, thanks for the help everyone. I rewrote the program (minus the main so that I can work on it) but am receiving an error message regarding my bool statements.
error C2511: 'empty' : overloaded member function 'bool (bool)' not found in 'Stack'
Code:
#include <iostream.h>
const int Stack_Size=100; /*The stack itself*/
class Stack{
public:
Stack(void);/*initialize the stack*/
void push (const int item); /*push an item*/
int pop (void);
int peek (const int item);
bool empty (const int item); /*is stack empty?*/
bool full (const int item); /* Is stack full*/
private:
int count; /*number of items in the stack*/
int data [Stack_Size]; /*The items themselves*/
};
Stack::Stack()
{
count=-1; /*initialize the top of the stack*/
}
void Stack::push(const int item)
{
count++;
data[count]=item;
}
int Stack::pop()
{
int value;
value=data[count];
count--;
return(value);
}
bool Stack::empty()
{
if (count==-1)
return TRUE;
else
return FALSE;
}
bool Stack::full()
{
if (count==Stack_Size-1)
return TRUE;
else
return FALSE;
}
-
looks good. You can either put all of that before main() or put the first part (class Stack;) in a header file called Stack.h and the rest in a cpp file called Stack.cpp and then list an include like this:
#include "Stack.h" at the start of the program and not list all the stuff before main(). Either way, time to start redoing the driver program (main()) to test you Stack class.
-
Disable Smilies in This Post
Please remember this when posting C++ code