With reading numbers without pressing "enter" in C language?

Asked

Viewed 95 times

0

I’m writing a program, and I have a little problem. When choosing one of the options you must press "enter" to proceed, however when pressing "enter" it already enters the next screen, thus skipping the first printf function where I should write the login name, thus going straight to password... Follow the example of the code:

int numero;
printf("\n\t\t MENU PRINCIPAL\n\n1- Estoque\n2- Loja\n3- Sair\n\nEscolha a opcao desejada: ");
scanf("%d", &numero);
fflush(stdin); // Limpar o buffer do teclado
switch (numero) {
case 1:
    system("cls");
    //Estoque();
    break;
case 2:
    system("cls");
    Loja();
    break;
case 3:
    system("cls");
    system("EXIT");
    return 0;
default:
    printf("Opcao invalida\n\n");
}

}

Shop() { char c; char cadastro_login[30]; char cadastro_password[30]; char login[30]; char password[30]; int a = 0, b = 1, d = 3;

printf("\n\t\t CADASTRO\n\n");
fflush(stdin); 
printf("Crie um login: ");
fflush(stdin); 
gets(cadastro_login);
printf("\nCrie uma senha: ");
do {
    c = _getch();
    if (isprint(c)) {
        cadastro_senha[a] = c; 
        a++;
        printf("*");
    }
    else if (c == 8 && a) { 
        cadastro_senha[a] = '\0';
        a--;
        printf("\b\b");
    }
} while (c != 13); 
cadastro_senha[a] = '\0';
printf("\n\n\t\t Cadastro efetuado com sucesso!\n\n");
system("cls");
do {
    printf("\nLogin: ");
    fflush(stdin);
    gets(login);
    printf("\nSenha: ");
    a = 0;
    do {
        c = _getch();
        if (isprint(c)) {
            senha[a] = c;
            a++;
            printf("*");
        }
        else if (c == 8 && a) {
            senha[a] = '\0';
            a--;
            printf("\b\b");
        }
    } while (c != 13);
    senha[a] = '\0';

    if (!strcmp(cadastro_login, login) && !strcmp(cadastro_senha, senha)) {
        printf("\n\n\t\t LOGIN EFETUADO COM SUCESSO!\n\n");
        b = 0;
    }
    else {
        system("cls");
        d--; 
        if (d)printf("\n\n\t\a\t SENHA INVALIDA - TENTE NOVAMENTE... \n\n");
    }
} while (b && d); 
if (!d)printf("\a\n\n\t\tCONTA BLOQUEADA - VOCE ERROU 3 VEZES CONSECUTIVAS.\n\n"); 
system("pause");
system("cls");

}

  • @Rafhainiveronezi WOW!!! vlw even!! Thank you very much, it worked correctly.

1 answer

-1

You can use a getchar(), right after the scanf.

printf("\n\t\t MENU PRINCIPAL\n\n1- Estoque\n2- Loja\n3- Sair\n\nEscolha a opcao desejada: ");
scanf("%d", &numero);
**getchar();**

Browser other questions tagged

You are not signed in. Login or sign up in order to post.