Use a do...while statement to create a menu system - C Statement

C examples for Statement:do while

Description

Use a do...while statement to create a menu system

Demo Code

#include <stdio.h>

int get_menu_choice( void );

int main( void )
{
    int choice;/*  ww  w.  j a va2  s. c  o  m*/

    choice = get_menu_choice();

    printf("You chose Menu Option %d\n", choice );

    return 0;
}

int get_menu_choice( void )
{
    int selection = 0;

    do
    {
        printf("\n" );
        printf("\n1 - Add" );
        printf("\n2 - Change");
        printf("\n3 - Delete");
        printf("\n4 - Quit");
        printf("\n" );
        printf("\nEnter a selection: " );

        scanf("%d", &selection );
     }while ( selection < 1 || selection > 4 );

     return selection;
}

Result


Related Tutorials