get something error with my code.
This is my code for my homework ( Pop and push from stack)
i get something wrong with this,
why when i make the 2nd time selection, the program will crash??
anyone can tell me or help me edit it to the correct 1??
Code:
#include <stdio.h>
#include <string.h>
#include <stack>
struct stack{
char *storage [50];
int top;
};
bool stk_isFull(stack * s){
if( s->top == 50){
return true;
}else{
return false;
}
}
void stk_init( stack * s){
s->top = 0;
}
void stk_push(stack*s){
char *item;
if(stk_isFull(s)){
printf("stack is full\n\n");
}else{
printf(" KEY IN THE ITEM YOU WANT TO PUSH\n\n");
scanf("%s",&item);
s->storage[s->top] = item;
(s->top)++;
printf(" %c has been pushed\n\n",item);
}
}
bool stk_isEmpty( stack*s){
if(s->top == 0)
return true;
else
return false;
}
void stk_pop(stack *s){
if (stk_isEmpty(s)){
printf("Stack is Empty.");
}else{
printf("%c has been popped!",s->storage[s->top]);
s->top = s->top - 1;
}
}
void stk_print(stack *s){
int x;
for(x = s->top; x > 0; x--){
printf("%d",s->storage[x]);
}
}
void menu(){
printf("PLEASE SELECT YOUR OPTION\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
printf("4.QUIT\n");
printf("Please Choose It:");
}
bool exit(){
char select[4];
printf( "\nType End To Exit " );
scanf( "%s", select );
if( strcmp( select, "END") == 0 || strcmp( select, "end" ) == 0 )
return true;
else {
return false;
}
}
void main (){
stack stk;
char *item;
stk_init(&stk);
int sel;
menu();
scanf("%d", &sel);
while(1){
switch(sel){
case 1:
stk_push(&stk);
break;
case 2:
stk_pop(&stk);
break;
case 3:
stk_print(&stk);
break;
case 4:
exit();
break;
default:
printf("\n\nNOTHING HAS BEEN CHOOSE");
}
menu();
scanf("%d",sel);
}
}