Thread: Menu problems....

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Menu problems....

    Hi;

    I having a problem with those menu, when i choose in the first menu an option to go to another submenu it works well, if i use the option to return to last menu the option works but puts invalid option....i can't choose another option..

    main menu
    Code:
    void menu_principal(void)
    {
    	char ch;
    	printf("BEMVINDOS AO BUZ \n");
    	printf("1 - Entrar como Administrador \n");
    	printf("2 - Entrar com Jogador \n");
    	printf("3 - Sair da aplicacao \n");
    	printf("Escolha uma das seguintes opcoes \n");
    	ch = getchar();
    	switch(ch)
    	{
    	case '1':
    	adm();
    	break;
    	case '2':
    	jogo();
    	break;
    	case '3':
    	//
    	break;
    	default :
    	printf("Opcao invalida");
    	}
    }
    2º menu

    Code:
    void jogo(void)
    {
    	char ch;
    	fflush(stdin);
    	printf("1 - Jogo curto (2 perguntas) \n");
    	printf("2 - Jogo medio (4 perguntas) \n");
    	printf("3 - Jogo grande (8 perguntas)\n");
    	printf("4 - Voltar ao menu inicial \n");
    	ch = getchar();
    	switch(ch)
    	{
    	case '1':
    	gp1_Ques();
    	break;
    	case '2':
    	printf("jogo 4()");
    	break;
    	case '3':
    	printf("jogo 8()");
    	break;
    	case '4':
    	menu_principal();
    	break;
    	default :
    	printf("Opcao invalida");
    	}
    }
    So after entering in this last menu, if i want to use the option 4 to return to main menu i have this:
    BEMVINDOS AO BUZ
    1 - Entrar como Administrador
    2 - Entrar com Jogador
    3 - Sair da aplicacao
    Escolha uma das seguintes opcoes

    opcao invalida (invalida option)

    What's wrong??

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Most likely 'return' keys left in the buffer get picked up as the next input.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    FFS, your OP here is worse than your OP 3 days ago with the same code. All you've done is removed indentation and added a fflush(stdin).

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Return??

    I don't have any return...
    It's really strange....scanf and fgets....

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    return is the enter key. When you input one character on the keyboard you press two keys: the character key (for example 'k') and the enter key. The latter is left in the input buffer, so the next time you call getchar() it won't let you press anything because it already has the return key as input.

    Now, if you are thinking that the solution to this is to use fflush(stdin) you are wrong, because fflush is not defined to work on input streams.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Ok claudiu, i have already get it., now it's works.

    Code:
    void jogo(void)
    {
    	int input;
        printf("1 - Jogo curto (2 perguntas) \n");
    	printf("2 - Jogo medio (4 perguntas) \n");
    	printf("3 - Jogo grande (8 perguntas)\n");
    	printf("4 - Voltar ao menu inicial \n");
        printf("Escolha uma das seguintes opcoes \n");
        scanf( "%d", &input );
        switch ( input )
        {
            case 1:
                gp1_Ques();
                break;
            case 2:
                printf("Jogo 4");
                break;
            case 3:
                printf("Jogo 8");
                break;
            case 4:
                menu_principal();
                break;
            default:
                printf( "Bad input, quitting!\n" );
                break;
        }
        getchar();
    }
    Iv'e used getchar();

    Thanks

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Good stuff.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu Problems
    By ultrabot90 in forum C++ Programming
    Replies: 6
    Last Post: 11-18-2007, 07:18 AM
  2. Problems with a menu
    By L_U_K_E in forum C++ Programming
    Replies: 27
    Last Post: 04-28-2006, 10:45 AM
  3. Problems with my menu
    By Olidivera in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2005, 12:44 PM
  4. Some problems with my menu !
    By skywing in forum C Programming
    Replies: 4
    Last Post: 04-26-2004, 03:18 PM
  5. Menu problems
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2002, 10:41 PM