Thread: Menu driven program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    INDIA
    Posts
    13

    Question Menu driven program

    I am trying out a menu driven program which has following options:
    1.Factorial of a number
    2.Prime or not
    3.Odd or even
    4.Exit
    Once a menu item is selected the appropriate action should be taken and once the action is finished ,the menu should reappear.Unless the user selects the exit option the program should continue to work.
    I have typed the following program but the menu item does not reappear again.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void main(exitmenu)
    {
    int opt,p,j,x=1,num,i=2,eo;
    choice:
    while(x<4)
    {
    printf("\n1.Factorial of a number");
    printf("\n2.Prime number or not");
    printf("\n3.Odd or Even");
    printf("\n4.Exit");
    printf("\nEnter your choice");
    scanf("&#37;d",&opt);
    
    switch(opt)
    case 1:
    {
     p=1,j=1;
     while(j<=7)
     {
    	p=p*j++;
    	printf("\nFactorial of %d=%d",x++,p);
     }
     break;
    case 2:
    {
     printf("\nInput a number");
     scanf("%d",&num);
     while(i<num && num%i!=0)
     i++;
     if(num==i)
     printf("Prime number");
     else
     printf("Not a prime number");
     break;
    }
    case 3:
    {
    printf("\nInput a number");
    scanf("%d",&eo);
    if(eo%2==0)
    printf("\nNumber is even");
    else
    printf("\nNumber is odd");
    break;
    
    }
    
    
    
    case 4:
    exit(exitmenu);
    goto choice;
    }
    }
    }
    What will be the right one??
    Last edited by paushali; 11-26-2007 at 10:15 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main(exitmenu)
    This is all wrong.
    main returns an int, and the parameters are either
    int main ( int argc, char *argv )
    int main ( void )

    Rather than trying to cram everything into a single function, try something like
    Code:
    opt = getMenuChoice();  // prints the menu, reads input, returns result
    switch ( opt ) {
      case 1:
        doFactorial();
        break;
      case 2:
        doPrime();
        break;
      case 3:
        printf( "TODO - Odd or Even\n" );
        break;
    }
    If you start with all your cases as being a printf, then you can check that all your menu stuff works first without cluttering up the code.
    When it does work, you can then confidently move onto each sub-function and test each one of those in turn.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You're being bitten by scanf() leaving the newline in the stdin input buffer.

    Todd

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    INDIA
    Posts
    13
    What is the meaning of:
    dofactorial();

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Just eyeballing this code, option #2 won't work a second time through the code. Variable i will have a residual value from the first time through.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    INDIA
    Posts
    13
    Quote Originally Posted by Todd Burch View Post
    You're being bitten by scanf() leaving the newline in the stdin input buffer.

    Todd
    Then what should I do?

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by paushali View Post
    Then what should I do?
    Read the forum's FAQ on the best way to get input from the user.

    Todd

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And it does for all cases except 1.

    Your case-label is on the wrong side of the brace, by the way - not that it really makes any difference, but it looks neater if it's on the right side.

    Your indentation is horrible (as in, doesn't exist).

    Why do you have a exit followed by a goto? The goto will never be reached if exit() works, right?

    The return type of main is int - see the FAQ.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    INDIA
    Posts
    13
    Quote Originally Posted by matsp View Post

    Why do you have a exit followed by a goto? The goto will never be reached if exit() works, right?

    --
    Mats
    OK I have deleted that goto but still its not working.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And which (if any) of the other 6 replies did you follow the advice of?

    Did you look at your case 1: code to see if you could figure out why it's exiting ONLY when you choose that case, and not 2 or 3? [I mean, I didn't even write the code, and I spotted the error after half a minute or so]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What is the meaning of:
    > dofactorial();
    It means it is a function you should write to implement that part of your assignment.
    You can of course call it whatever you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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, 12:39 PM
  5. c++ program with a button menu
    By quakegod in forum C++ Programming
    Replies: 0
    Last Post: 01-07-2002, 01:09 AM