1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

making menu

Discussion in 'C' started by hoda, Jul 17, 2009.

  1. hoda

    hoda New Member

    Joined:
    Feb 17, 2009
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    what is the code in c programing to make menu for 4 function and the fifth one to exit?
    is it possible to go up and down by using keyboard then choose between menus by pressing enter?
    in the first page, menu is printed and when i choose one of them all of the page become clear and chosen function works.
     
  2. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Using Switch you can achieve that.

    Example :

    Code:
    
    #include <stdio.h>
    
    int main(void)
    {
        float numb1 = 0, numb2 = 0;       /* the two numbers to work on */
        int menu = 1;             /* add or substract or divide or multiply */
        float total = 0;           /* the result of the calculation */
        char calType;             /* what type of calculation */
    
        printf("Please enter in the first of the two numbers\n\t");
        scanf("%f", &numb1);                 /* READ first number */
    
        printf("\n\nPlease enter the second of the two numbers\n\t");
        scanf("%f", &numb2);                 /* READ second number */
    
        printf("\n\nWhat would you like to do?\n\n");    /* WRITE instructions */
        printf("\t1 = add\n");
        printf("\t2 = substract\n");
        printf("\t3 = multiply\n");
        printf("\t4 = divide\n");
    
        printf("\n\nPleas make your selection now:\n\t");
        scanf("%d",&menu);                   /* READ calculation type */
    
        switch (menu)                  /* select the type of calculation */
        {
        case 1: total = numb1 + numb2;
            calType = '+';             /* assign a char to symbolise calculation type */
            break;
        case 2: total = numb1 - numb2;
            calType = '-';
            break;
        case 3: total = numb1 * numb2;
            calType = '*';
            break;
        case 4: total = numb1 / numb2;
            calType = '/';
            break; 
        default: printf("Invalid option selected\n");
        }
    
        if (menu == 3 && numb2 == 0)            /* cannot divide by 0 */
            printf("\n\n\tYou cannot divide by 0\n\n");
    
                  /* display result to 2 decimal places */
        printf("\n\n*************************");
        printf("\n\n\t%.3f %c %.3f = %.2f", numb1, calType, numb2, total);
        printf("\n\n*************************\n\n");
    
        return 0;
    
    }
    
    
    
    
     

Share This Page