Thread: Switches and default

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    Switches and default

    Hello

    I wrote this program:

    Code:
    
    int selection;
    
    printf("1.)   Integer to Binary converter\n");
    printf("2.)   Fibonacci generator\n");
    printf("999.) Exit\n");
    printf("Enter selection ---> ");
    scanf("%d", &selection);
    
    switch (selection){
    
      case 1:
       printf("You selected 1\n");
       break;
    
      case 2:
       printf("You selected 2\n");
       break;
    
      case 999:
       printf("Goodbye\n");
       break;
    
      default:
       printf("Wronge selection\n");
       break;
    
                       }


    This program needs a lot more work to be finished, but how can I make it that when the default switch gets selected the top menu gets repeated until that the user selects 1,2, or 999?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    You could put part of the code that's displaying menu-info into separate function, eg.

    Code:
    void printMenu()
    {
       printf("1.)   Integer to Binary converter\n");
       printf("2.)   Fibonacci generator\n");
       printf("999.) Exit\n");
       printf("Enter selection ---> ");
    }
    and later on ask user for input in while ( 1 ) loop. If it reaches default in switch, you simply call printMenu().

    Code:
    default:
       printf("Wronge selection\n");
       printMenu();

  3. #3
    Chinese pāté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    One way to resolve your problem is changing your piece of code for something like this
    Code:
    int i;
    // ...
    
    do
    {
        printf("1.)   Integer to Binary converter\n");
        printf("2.)   Fibonacci generator\n");
        printf("999.) Exit\n");
    
        printf("Enter selection ---> ");
        i = scanf("%d", &selection);
        while (getchar() != '\n');                   // flush the input buffer
    } while (i != 1 && selection != 1 && selection != 2 && selection != 999);
    
    // you put the switch instruction here
    // note that selection WILL be 1, 2 or 999

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    I changed my program to :

    Code:
    #include <stdio.h>
    
    int printMenu(int menu)
    {
       printf("1.)   Integer to Binary converter\n");
       printf("2.)   Fibonacci generator\n");
       printf("999.) Exit\n");
       printf("Enter selection ---> ");
       scanf("%d", &menu);
    
       return menu;
    }
    
    main(){
    
    int selection;
    
    selection = printMenu(selection);
    
    switch (selection){
    
      case 1:
       printf("You selected 1\n");
       break;
    
      case 2:
       printf("You selected 2\n");
       break;
    
      case 999:
       printf("Goodbye\n");
       break;
    
      default:
       printf("Wronge selection\n");
       break;
       printMenu();
                       }
    
        }

    but by putting
    Code:
    printMenu();
    in the default section of the switch it gives compile errors. How can I call the menu from the default switch?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean something along the lines of "unreachable code", because you put the function call AFTER the break statement?

    Or something else (be specific!).
    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.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    48
    Quote Originally Posted by Salem View Post
    You mean something along the lines of "unreachable code", because you put the function call AFTER the break statement?

    Or something else (be specific!).
    oops I never realized its after break. after putting "printMenu();" in the default section of the switch I get compile errors like:

    1.c: In function āmainā:
    1.c:36: error: too few arguments to function āprintMenuā

    I just want to call the menu again when the switch goes to default. How can I do that?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > selection = printMenu(selection);
    Here you call it with one argument.

    > printMenu();
    And here you don't.

    Now exactly what part of "too few arguments" is confusing you?
    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.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int printMenu(int menu)
    {
       printf("1.)   Integer to Binary converter\n");
       printf("2.)   Fibonacci generator\n");
       printf("999.) Exit\n");
       printf("Enter selection ---> ");
       scanf("&#37;d", &menu);
    
       return menu;
    }
    This function does not need to have an int argument passed in. Having one just so you can read a value into it and then return that value isn't the best use of a function argument. Ditch the argument and make the variable local:
    Code:
    int printMenu()
    {
        int menu;
       printf("1.)   Integer to Binary converter\n");
       printf("2.)   Fibonacci generator\n");
       printf("999.) Exit\n");
       printf("Enter selection ---> ");
       scanf("%d", &menu);
    
       return menu;
    }
    Then just always call the function without an argument:
    Code:
    selection = printMenu();
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And the above code [by original poster] still misses the while-loop to "go back and do it again" when the selection is wrong.

    I'd prefer to see the "PrintMenu" function at the begging of some sort of loop, and not in the "Your selection is wrong" default statement. That way, the user can also select another choice after the first choice is finished [e.g. select 1 first time, then 2 for the second choice, and 999 to exit].

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  2. Switches and functions
    By MyntiFresh in forum C++ Programming
    Replies: 24
    Last Post: 07-17-2005, 01:16 PM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  5. Sprint Exposed: News
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-29-2002, 02:17 AM