Thread: program won't exit

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    17

    program won't exit

    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!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Programs that are menu-driven usually have a do-while structure in main, example in pseudocode:

    Code:
    int user_menu (void)
    {
        /* here you get the user selection and parse it with the switch statement */
        if (user_input == quit) {
            return -1;
        return 0;
    }
    
    int main(void)
    {
        do {
             
        } while (user_menu() > 0);
    }
    You might want to re-structure your program accordingly.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Thank you very much for the reply, but my teacher wants us to use a while loop for our menu like I have it, so I cannot change the structure of the menu.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Well, to fix the problem of it not exiting, you could put a "return;" after you call menu() if QUIT is true.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    she actually mentioned something like that. She said she doesn't want us to use a "forced" exit like a return statement or an exit statement. Thank you for the reply though. Can anyone see why my while loop is not ending when quit is switched to TRUE? that is the answer I am looking for if anyone sees it... Thank you!

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Heh, you'll kick yourself.....

    Code:
     case '4':
     quit = TRUE;
    Your "4" is in single quotes, meaning the character 4 not the number.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tkd_aj
    my teacher wants us to use a while loop for our menu like I have it, so I cannot change the structure of the menu.
    You declared the menu function to return an int, so if that is what your teacher specified, then it makes sense to also read the input in the menu function and return it. This way, you would write:
    Code:
    #define QUIT_OPTION 4
    
    /* ... */
    
    int option;
    while ((selection = menu()) != QUIT_OPTION) {
        switch (selection) {
            /* ... */
        }
    }
    The menu function would handle the case of an invalid selection.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by smokeyangel View Post
    Heh, you'll kick yourself.....

    Code:
     case '4':
     quit = TRUE;
    Your "4" is in single quotes, meaning the character 4 not the number.
    Wow... lol.. Thank you! That was it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exit program
    By Teardrop3903 in forum C Programming
    Replies: 7
    Last Post: 03-11-2011, 01:06 AM
  2. -1 to exit program ?
    By sharris in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2010, 09:14 PM
  3. How to exit program???
    By spottedzebra in forum C Programming
    Replies: 10
    Last Post: 06-21-2010, 04:43 PM
  4. Before your program does exit(0);
    By ycode in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:47 PM
  5. exit program
    By spentdome in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 02:08 PM

Tags for this Thread