Ok, I changed his code to this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void flush_newline(char *);
int process_choice(char *);
int valid_menu_choice(char *);
int main()
{
char line[5];
do{
printf("[1] Cadastrar cliente\n");
printf("[2] Visualizar ficha\n");
printf("[3] Sair\n\n");
printf("> ");
fgets(line, (sizeof line) - 1, stdin);
flush_newline(line);
}while(process_choice(line) != 3);
return 0;
}
/*______________________________________________________________________________________
| flush_newline(): retira o \n do final da string se o mesmo existir ou limpa o buffer. |
| |
| Parâmetros: a string a ser testada. |
|_______________________________________________________________________________________|*/
void flush_newline(char *line)
{
char ch;
char *pointer;
if((pointer = strchr(line, '\n')) != NULL)
*pointer = '\0';
else{
while((ch = getchar()) != '\n')
ch = getchar();
}
}
/*______________________________________________________________________________________________
| process_choice(): Valida e, se tudo estiver certo, processa a opção escolhida pelo usuário |
| no menu principal. |
| |
| Parâmetros: |
| line: texto digitado pelo usuário no prompt do menu principal. |
| |
| Retorna: |
| Se o input do usuário não for uma opção válida, a função retorna o número 0. Dessa forma, |
| o menu e o prompt vão ser mostrados outra vez na tela por causa do loop na função main(). |
| |
| Se o input for válido e igual ao número 3, o número 3 é retornado e o programa termina. |
|_______________________________________________________________________________________________|*/
int process_choice(char *line)
{
int menu_choice;
if((menu_choice = valid_menu_choice(line)) == 0)
return 0;
else if(menu_choice == 1){
printf("Cadastrar cliente.\n\n");
return 0;
}
else if(menu_choice == 2){
printf("Visualizar ficha.\n\n");
return 0;
}
else
return 3;
}
/*_________________________________________________________________________________________________
| valid_menu_choice(): Valida o input do usuário de acordo com as opções do menu principal. |
| |
| Parâmetros: |
| line: texto digitado pelo usuário no prompt do menu principal. |
| |
| Retorna: |
| Se o input do usuário não for válido, a função retorna o número 0, o que vai causar a função |
| process_choice() retornar 0 também. O menu e o prompt vão ser mostrados outra vez na tela. |
| |
| Se o input for válido, ele será convertido para int e retornado. |
|__________________________________________________________________________________________________|*/
int valid_menu_choice(char *line)
{
size_t i;
int menu_choice;
if(line[0] == '\0'){
printf("Comando desconhecido.\n\n");
return 0;
}
for(i = 0; i < strlen(line); i++){
if(!isdigit(line[i])){
printf("Comando desconhecido.\n\n");
return 0;
}
}
sscanf(line, "%d", &menu_choice);
if((menu_choice <= 0) || (menu_choice > 3)){
printf("Comando desconhecido.\n\n");
return 0;
}
return menu_choice;
}
It worked, but now there's just a little detail. Look at this:
Code:
[1] Cadastrar cliente
[2] Visualizar ficha
[3] Sair
> 111111111111111
Comando desconhecido.
[1] Cadastrar cliente
[2] Visualizar ficha
[3] Sair
> 111111111111111111111111
Comando desconhecido.
[1] Cadastrar cliente
[2] Visualizar ficha
[3] Sair
>
If I start the program, type a bunch of ones and press enter, the error message will be displayed ("Comando desconhecido"). If I do the same thing on the second prompt, I have to hit enter twice in order to get the error message, and it stays like that until I input something good. Why is it happening? Sorry about the silly questions