Presents a menu of choices, gets the user's choice, and then uses the switch statement to execute - C Statement

C examples for Statement:switch

Description

Presents a menu of choices, gets the user's choice, and then uses the switch statement to execute

Demo Code

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

int main()//from   www .j  ava2 s  .  c om
{
    int choice;

    printf("1. Add New Contact\n");
    printf("2. Edit Existing Contact\n");
    printf("3. Call Contact\n");
    printf("4. Text Contact\n");
    printf("5. Exit\n");

    do{
        printf("Enter your choice: ");
        scanf(" %d", &choice);
        switch (choice)
        {
            case (1): printf("\n one");
                    break;
            case(2): printf("\n two ");
                    break;
            case (3): printf("\n three ");
                    break;
            case (4): printf("\n four ");
                    break;
            case (5): printf("\n five ");
                    exit(1); 
            
            default: printf("\n%d is not a valid choice.\n", choice);
                    break;
         }
    } while ((choice < 1) || (choice > 5));

    return 0;
}

Result


Related Tutorials