Thread: switch case

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Question switch case

    hi everyone
    i was discovering a problem while making a menu for C.

    i tried to start the programm as follows:

    main ()

    {
    printf("Choose:\n");
    printf("1. Internet\n");
    printf("2. LAN\n");
    printf("3. Linux\n");
    printf("4. Windows\n");
    }

    i know that i missed it putting in the include files. but my question is how to define the variables.
    i would do it as it follows:

    {
    int a, b, c, d; /* for the different inputs like press 1, 2 and so on....*/

    i guess its wrong cause my compiler didnt like it.
    how do i define these variables?
    and how do i make a switch case for the four ones in here?
    maybe like this?

    switch
    case 1: printf("you selected blabla");
    case 2: printf("you selceted bla");


    any help is welcome
    threadhead

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Hope this helps something:

    Code:
    int input;
    
    input = get_input ();
    
    switch (input)
    {
        case 1:
            /* Do something */
            break;
        case 2:
            /* Do something */
            break;
        ....
        default:
            /* Do something */
            break;
    }

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    with the line

    'int input;' you define the variable right?

    but what do you with?

    'input = get_input ();'

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>'input = get_input ();'
    This is a call to a function that gets input from somewhere and assigns it to the input variable. It's a made up name use purely for the example. You can of course write a function called getinput(), which is what you would normally do.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >>'input = get_input ();'
    This is a call to a function that gets input from somewhere and assigns it to the input variable. It's a made up name use purely for the example. You can of course write a function called getinput(), which is what you would normally do.
    I'm amazed at how many times I'm asked to explain my use of something similar.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    
    int PrintMenu ( void );
    
    int main ( void )
    {
    	char choice;
    	while ( PrintMenu() == 0 )
    	{
    		choice = getchar();
    		switch ( choice )
    		{
    			case '1': printf("To be, or not to be.\n\n"); break;
    			case '2': printf("Hello, world.\n\n"); break;
    			case 27: printf("Closing.."); exit(0); break;
    			default: break;
    		}
    	}
    	return 0;
    }
    
    int PrintMenu ( void )
    {
    	printf("Choose a message to print:\n\n");
    	printf("1.  To be, or not to be.\n");
    	printf("2.  Hello, world.\n\n");
    	printf("Escape exits.\n\n");
    	return 0;
    }
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM