Q:

Complete Stack implementation in C

I have been asked to do a stack implementation. I need to the following functions;

Push
Pop
isFull
isEmpty
peek
Display the whole array

This is what I wrote. 
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

/* Stack Structure  */
struct stack
{
    int s[SIZE];
    int top;
}st;

int main()
{

int option;

printf("+-------------------------------------+\n");
printf("1.Push\n2.Pop\n3.Check whether the stack is full\n4.Check whether the stack is empty\n5.Check the Top Element\n6.Display the Stack\n7.Exit\n");
printf("+-------------------------------------+\n");
printf("Enter Choice:\t");
scanf("%d", &option);

while(option == -99)
{
    switch(option)
    {
    case 1:
        push();
        break;
    case 2:
        pop();
        break;
    case 3:
        isFull();
        break;
    case 4:
        isEmpty();
        break;
    case 5:
        peek();
        break;
    case 6:
        display();
        break;
    case 7:
        printf("You Exited from the program");
        break;
    }
}
return 0;
}

/*Function to add an element to the stack*/
void push ()
{
int num;
if (st.top == (SIZE - 1))
{
    printf ("Stack is Full\n");
}
else
{
    printf ("Enter the element to be pushed\n");
    scanf ("%d", &num);
    st.top ++;
    st.s[st.top] = num;
}
}

/*Function to delete an element to the stack*/
int pop()
{
int num;
if (st.top == -1)
{
    printf ("Stack is Empty\n");
    return st.top;
}
else
{
    num = st.s[st.top];
    printf ("Popped element is = %d", st.s[st.top]);
    st.top --;
}
return (num);
}

/*Function to Check whether the stack is full*/
 void isFull()
 {

   if(st.top == SIZE - 1)
  printf("Stack is Full");
   else
  printf("Stack has %d elements", st.top - 1);
}

/*Function to Check whether the stack is Empty*/
void isEmpty()
{
if(st.top == -1)
  printf("Stack is Empty");
else
  printf("Stack has %d elements", st.top - 1);
}

/* Function to display the top element*/
void peek()
{
printf("Top most element: \t%d", st.s[st.top]);
}

/* Function to display the stack*/
void display ()
{
int i;
if (st.top == -1)
{
    printf ("Stack is empty\n");
}
else
{
    printf ("\n The status of the stack is \n");
    for (i = st.top; i >= 0; i--)
    {
        printf ("%d\n", st.s[i]);
    }
}
printf ("\n");
}

0 errors, 11 warnings are shown. 
But when I run the programme it ends after asking the choice.
output:
+-------------------------------------+
1.Push
2.Pop
3.Check whether the stack is full
4.Check whether the stack is empty
5.Check the Top Element
6.Display the Stack
7.Exit
+-------------------------------------+
Enter Choice:   1

Process returned 0 (0x0)   execution time : 6.304 s
Press any key to continue.

I really need to have it done. It is one of my assignments. Please help me and thank you very much for your time. :-)

A:

You're exiting because you are entering an option that's between 1 and 7, but your while loop is checking for -99. So the while loop gets skipped and you exit.
I'm guessing what you actually want to do is keep prompting the user for actions until they exit. Try considering what functionality you actually want looped over in your program.
Also don't be afraid to put print statements in your code and trace the flow line by line. That will help you a lot with debugging.
Best of luck on the assignment!

