Hi,
This is a data structure using "Stack" for some reason "push" "pop" and "peekItemPostion" do not work properly. Could somebody try to help me, please? Thanks.
struct student
{
int sID;
};
typedef struct student S;
//----------------------------------------------------------------------------------------------------
struct stack
{
int count;
S student[5]; //Array
};
//Prototypes
void stackInit(stack * the_stack);
int push(stack * the_stack, int item);
int pop(stack *the_stack, int * item);
int NumberItems(struct stack *the_stack);
int peekItemPostion(struct stack *the_stack, int position );
int isEmpty(struct stack *the_stack);
int isFull(struct stack *the_stack);
int main()
{
S student [5];
struct student cArray[10];
struct stack a_stack;
stackInit(&a_stack); //initialise the stack
int num, num1, success;
cout << "All numbers entered will be added to the Stack"<<endl;
//remove items from the stack one at a time
//produces an error message if none present
int pop( struct stack *the_stack, int * item)
{
if (the_stack->count == 0)
{
cout << "No elements left"<<endl;
return 0;
}
else
{
the_stack->count--;
*item = the_stack->student[the_stack->count];
return 1;
}
}
//-----------------------------------------------------------------------------------------------------
int NumberItems(struct stack *the_stack)
{
return (the_stack->count + 1 );
}
//---------------------------------------------------------------
//get the value of an item at a given position
int peekItemPostion(struct stack *the_stack, int position )
{
return ( the_stack->student[position]);
}
Please provide a more detailed problem description.
See: http://www.cplusplus.com/forum/beginner/1/#msg6680
Excerpt:
When asking about code
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.
That said, I do see some problems. In several places you are treating student objects as though they are merely an integer.
The student structure also contains string members. From your code:
Code like line 75 from push(): the_stack->student[the_stack->count ++] = item; //add item
should produce an error. Are you getting compiler errors?
This should work though: the_stack->student[the_stack->count ++].sID = item; //add item
Here an integer data member of the student structure is assigned a value.
There are several places in your code with problems like this.