Thread: Menu displays but does not accept selection

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Menu displays but does not accept selection

    I am writing a stacks program that is menu driven. The user has 4 options (Push, Pop, Print and Quit). I'm not certain all of my code is right yet and may require additional tweeking, but the problem I am encounting right now is with the menu.

    When I run the program the menu displays and when I enter a selection, the menu displays again and does not do anything based on my selection. I can enter any selection number and the menu simply displays again... Help is appreciated! Thank you!

    MAIN.C
    Code:
    #include <stdio.h>
    #include "boolean.h"
    #include "stack.h"
    
    int main(void) {
    
      char choice;
      boolean quit = 0;
      int data_item;
      int data_popped;
      stack top;
    
    init_stack(&top);
    
    
    while (quit == 0){
    
        printf("1 Push\n");
        printf("2 Pop\n");
        printf("3 Print\n");
        printf("4 Quit\n\n");
        printf("Enter Selection: ");
    
        scanf(" %c", &choice);
        printf("\n");
      }
    
       switch (choice){
           case '1':
           if (!is_full()) {
           printf("Enter the data\n");
           scanf("%d", &data_item);
             push (&top, data_item);
           }
    
           else {
           printf("The Stack is Full\n");
           }
    
           break;
    
           case '2':
           if (!is_empty(top)) {
             data_popped = pop(&top);
             printf("&d was removed", data_popped);
           }
    
           else {
             printf("The Stack is Empty\n");
           }
           break;
    
           case '3':
           if (!is_empty(top)){
             print_stack(top);
           }
           else {
             printf("The Stack is Empty\n");
           }
           break;
     
           case '4':
            if(quit == 0){
            quit = 1;
          }
           break;
    
           default:
           printf("Invalid Selection\n");
           break;
    
       }     
    }
    STACK.C
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    
    void init_stack(stack *s) {
      (*s) = NULL;
    }
    
    
    boolean is_full(void) {
      stack temp;
      temp = (stack) malloc (sizeof(struct stacknode));
      if (temp == NULL)
        return TRUE;
      else {
        free (temp);
        return FALSE;
      }
    }
    
    
    boolean is_empty(stack s) {
      if (s == NULL)
        return TRUE;
      else
        return FALSE;
    }
    
    
    void push(stack *s, int x){
      stack temp;
      temp = (stack) malloc(sizeof(struct stacknode));
      temp -> data = x;
      temp -> next = (*s);
      (*s) = temp;
    }
    
    
    int pop(stack *s) {
      stack temp;
      int data_popped;
      temp = *s;
      data_popped = temp -> data;
      *s = temp -> next;
      free (temp);
      return data_popped;
    }
    
    
    void print_stack(stack s) {
      if (!is_empty(s)) {
        printf("%d\n", s->data);
        print_stack(s->next);
      }
    }
    STACK.H

    Code:
    #ifndef STACK_H
    #define STACK_H
    
    typedef struct stacknode{
      int data;
      struct stacknode *next;
    } *stack;
    
    typedef int boolean;
    #define TRUE 1
    #define FALSE 0
    
    
    void init_stack(stack *);
    boolean is_full(void);
    boolean is_empty(stack);
    void push(stack *, int);
    int pop(stack *);
    
    #endif
    BOOLEAN.H
    Code:
    #ifndef BOOLEAN_H
    #define BOOLEAN_H
    
    typedef int boolean;
    
    const boolean FALSE = 0;
    const boolean TRUE = 1;
    
    #endif

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at where your closing brace for the menu loop is. It's on line 26, before your switch statement. Your loop only ever prints the menu and reads choice.

    Other things I noticed:

    • You declare TRUE and FALSE, but always compare your booleans to the literal 0. Either use the words or don't bother declaring them.
    • Your printf on line 45 in main is using "&d", it should be "%d".
    • You should return an integer from the end of main, usually 0 for success.
    • You're missing a prototype for print_stack in stack.h.
    • All your function prototypes in stack.h should have the keyword 'extern' in front of them.
    • You unnecessarily redefine boolean, FALSE and TRUE in stack.h. This causes problems when linking multiple files.
    • You shouldn't define FALSE and TRUE variables in boolean.h. It causes problems when linking multiple files. Instead, replace them with #define statements, or move the definition into a boolean.c file and declare
      them extern in boolean.h (see below for example and definitions).


    Definition: This actually creates the variable, and sets aside space for it. This can be done only once for each variable in each scope (i.e. you can only define a global constant like FALSE once). Defining a function means providing the function body.

    Declaration: This simply describes the variable, i.e. it's name and type. A declaration of a function is a function prototype. It describes the return type, and the count and types of parameters. For global symbols (global vars and functions), the declaration can be qualified for linkage with the keywords 'extern' (meaning it is defined in a different file) or 'static' meaning it is only visible within this file. A declaration of a variable can not be initialized.

    Example:
    boolean.h
    Code:
    #ifndef BOOLEAN_H
    #define BOOLEAN_H
    
    typedef int boolean;
    
    extern const boolean FALSE;
    extern const boolean TRUE;
    
    #endif
    boolean.c
    Code:
    const boolean FALSE = 0;
    const boolean TRUE = 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Displays menu twice after making a selection
    By jacobj86 in forum C Programming
    Replies: 4
    Last Post: 03-08-2012, 02:03 PM
  2. Folder selection menu using Common Dialog Box?
    By Devil Panther in forum Windows Programming
    Replies: 6
    Last Post: 04-29-2007, 01:09 PM
  3. Program using menu selection and subprograms
    By hopeolicious in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 12:36 PM
  4. menu selection trouble
    By Spectrum48k in forum C Programming
    Replies: 13
    Last Post: 11-20-2002, 11:06 PM
  5. Menu Selection
    By Ruski in forum C++ Programming
    Replies: 13
    Last Post: 10-12-2002, 09:11 PM