Creating a Main Menu

Avoid clutter in the main() function

3-minute read
Made by ChickenFryBytes Studios
Table of Contents

The main() function is the entry point to our program. As such, we try our best not to clutter this function with much code. Instead, it is better to place the main menu here and call other functions from this main menu. Here is a possible implementation for the main menu:

#include <stdio.h>
#include <stdlib.h>

void clear_stdin() {
  int ch;
  while ((ch = getchar()) != '\n' && ch != EOF)
    ;
}

void feature_1() { printf("Using feature 1...\n"); }

void feature_2() { printf("Using feature 2...\n"); }

void feature_3() { printf("Using feature 3...\n"); }

int main() {
  int choice;

  // label to be used with 'goto' statement
present_choice:
  printf("\nšŸ“ MAIN MENU šŸ“\n");
  printf("------------------------\n");
  printf("šŸ“Œ 1 - Use feature 1\n");
  printf("šŸ“Œ 2 - Use feature 2\n");
  printf("šŸ“Œ 3 - Use feature 3\n\n");

  printf("Please enter a choice: ");
  scanf("%d", &choice);

  switch (choice) {
  case 1:
    feature_1();
    break;
  case 2:
    feature_2();
    break;
  case 3:
    feature_3();
    break;
  default:
    printf("\nāŒ Option does not exist! Please enter a valid option!\n");
  }

  // remove any characters left in stdin
  clear_stdin();
  goto present_choice;
  return 0;
}
menu.c
Copy

Clearing standard input

The standard input (stdin) buffer will contain extra characters not handled by scanf. Leaving these characters in buffer will cause future calls to scanf to use the characters as if the user had input them into these newer calls. Thus we write a function to clear this buffer:


void clear_stdin() {
  int ch;
  while ((ch = getchar()) != '\n' && ch != EOF);
}

Example use

Here is how you may structure the main menu code for a library management software:

#include <stdio.h>
#include <stdlib.h>

void clear_stdin() {
  int ch;
  while ((ch = getchar()) != '\n' && ch != EOF)
    ;
}

void add_new_book() {
  printf("\nPlease enter the details of the book:\n(*enters book details*)\n");
}

void see_book_list() {
  printf("\nThe books are:\n- Moby Dick\n- To Kill A Mockingbird\n- Mastering "
         "Go\n");
}

void see_outstanding_books() {
  printf("\nThe list of outstanding books is:\n- To Kill A Mockingbird\n");
}

int main() {
  int choice;

  // label to be used with 'goto' statement
present_choice:
  printf("\nšŸ“ MAIN MENU šŸ“\n");
  printf("------------------------\n");
  printf("šŸ“Œ 1 - Add new book\n");
  printf("šŸ“Œ 2 - See book list\n");
  printf("šŸ“Œ 3 - See list of all outstanding books\n\n");

  printf("Please enter a choice: ");
  scanf("%d", &choice);

  switch (choice) {
  case 1:
    add_new_book();
    break;
  case 2:
    see_book_list();
    break;
  case 3:
    see_outstanding_books();
    break;
  default:
    printf("\nāŒ Option does not exist! Please enter a valid option!\n");
  }

  // remove any characters left in stdin
  clear_stdin();
  goto present_choice;
  return 0;
}
menu-2.c
Copy

Notice how not much is changed besides the names and bodies of the feature functions, and the messages listed to the user in the main() function before the switch-case statement.


Like our content? Support us via Donations