Stack is not working properly

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;

while( isFull(&a_stack) !=1 ) // !isFull(&a_stack)
{
cout << endl<<"Enter a number"<<endl;
cin >> num1;
success= push( &a_stack, num1);
}
int item;

while( isEmpty( &a_stack) !=1 ) //check for values on stack
{
num= pop(&a_stack, &item);
cout << item <<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
//----------------------------------------------------------------------------------------------------
//Initialise stack showing number of items on stack

void stackInit(struct stack * the_stack)
{
the_stack->count =0;
}
//----------------------------------------------------------------------------------------------------

//add items to stack
int push( struct stack * the_stack, int item)
{

if ( the_stack->count < 5)
{
the_stack->student[the_stack->count ++] = item; //add item

return 1;
}
else

return 0;
}
//----------------------------------------------------------------------------------------------------

//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]);
}

//---------------------------------------------------------


//returns 1 if empty or 0 otherwise
int isEmpty(struct stack *the_stack)
{
if ( the_stack->count == 0)
return 1;
else
return 0;
}
//----------------------------------------------

//returns 1 if full or 0 otherwise
int isFull(struct stack *the_stack)
{
if (the_stack->count == 5)
return 1;
else
return 0;
}

[/code]
Last edited on
closed account (D80DSL3A)
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:
1
2
3
4
5
6
7
8
9
struct student
{
       int sID;
       string sName;
       string add;
       string cCode;
       string courseN;
       
};

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.
Thank you for your time, it is working properly now!
Topic archived. No new replies allowed.