I am pretty new to C programming and this is a homework assignment.
I do not understand why my program will not exit. I looked at a previous program I wrote for this same class and it literally looks the exact same but the old one works and this one does not. Here is my code. Everything except the quit works. Whenever I choose option 4 for quit I get the default statement for some reason and then it loops and shows the menu over and over again.
Code:
#include <stdio.h>
#include <stdlib.h>
#include "boolean.h"
#include "stack.h"
int menu(void);
int main(void){
boolean quit=FALSE;
int selection, num;
stack top;
init_stack(&top);
menu();
while (!quit){
scanf("%d", &selection);
switch (selection){
case 1:
if (is_full()){
printf("Cannot add another value. It is already full\n");
}
else{
printf("Please enter the number you would like to add\n");
scanf("%d", &num);
push(&top, num);
}
break;
case 2:
if (is_empty(top)){
printf("Cannot remove a value. There are none to remove.\n");
}
else{
num = pop(&top);
printf("%d has been removed from the stack\n", num);
}
break;
case 3:
if (is_empty(top)){
printf("There is nothing to print\n");
}
else{
print_stack(top);
}
break;
case '4':
quit = TRUE;
break;
default:
printf("Error: that is not a valid selection. Please try again.\n");
break;
}
if (!quit){
menu();
}
}
printf("Have a nice day!");
}
int menu(){
printf("Please select from one of the following options:\n");
printf("1. Push\n2. Pop\n3. Print\n4. Quit\n\n");
printf("Please enter your selection ");
}
Thank you for any help!