menu driven program help

This is a discussion on menu driven program help within the C Programming forums, part of the General Programming Boards category; hi, could someone please help me, i am trying to write a menu driven c program using switch case. its ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    menu driven program help

    hi, could someone please help me,
    i am trying to write a menu driven c program using switch case.
    its a program which should have menu as 1.add, 2.sub, 3.mul, 4.div
    i wrote this but cannot get output... someone please correct me...


    [code]
    #include <stdio.h>
    void main()
    {
    int opt,a,b,sum,sub,mul,div;
    printf("\n1.add");
    printf("\n2.sub");
    printf("\n3.mul");
    printf("\n4.div");
    printf("\nenter your choice");
    scanf("%d",&opt);
    switch(opt)
    {
    case 1:
    case add:
    printf("enter a & b values");
    scanf("%d%d",&a,&b);
    sum=a+b;
    printf("%d",sum);
    break;
    case 2:
    case sub:
    printf("enter a & b values");
    scanf("%d%d",&a,&b);
    sub=a-b;
    printf("%d",sub);
    break;
    default:
    printf("enter correct option");
    }
    }
    [\code]




    please help

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I won't fix it for you but I will suggest you take a very close look at the case statements in your switch.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, vallamrao!

    A few suggestions, if I may:

    1) Use code tags (highlight your code and click on the # icon in the editing window). It's almost impossible to enjoy studying code that's all smashed up as html text.

    2) Instead of repeating scanf() for a and b in your cases, why not do it before the switch case starts, and then inside the case statement, just do the actual operation on a and b.

    3) Look at them cases! Well, well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. menu driven interface
    By weegi in forum C Programming
    Replies: 4
    Last Post: 04-17-2004, 04:08 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. c++ program with a button menu
    By quakegod in forum C++ Programming
    Replies: 0
    Last Post: 01-07-2002, 12:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21