char returns invalid typed number as option

Asked

Viewed 50 times

1

opa good afternoon, I am developing a menu in which the option typed is a char, the same returns the default correctly after typing a letter, however, my switch has 7 case options ('1', '2',...,'7'), the problem is that when I type an invalid number as for example: 22, it returns the case '2' (I don’t know what happens with the other 2), being that it should return the default, can anyone help me identify the error? (I’m not able to think of what could be). follows the code to analyze.

#include <stdio.h>
int main() {
    
    char op;
    char opc = 's';
    char *diaSemana[7] = {"\n1. Segunda-feira", "\n2. Terca-feira", "\n3. Quarta-feira", "\n4. Quinta-feira", "\n5. Sexta-feira", "\n6. Sabado", "\n7. Domingo"};
    
    while (opc == 's') {
        
        printf("\n------Menu------\n");
        
        for (int i = 0; i < 7; ++i) {
            printf("%s\n", diaSemana[i]);
        }

        printf("\nEntre com uma opcao.\n");
        fflush(stdin);
        scanf("%c", &op);
        switch(op) {
            case '1':           
                printf("\nDia da semana:%s\nCor da camisa: branca.\n", diaSemana[0]);
            break;
            case '2':   
                printf("\nDia da semana:%s\nCor da camisa: verde.\n", diaSemana[1]);        
            break;
            case '3':   
                printf("\nDia da semana:%s\nCor da camisa: azul.\n", diaSemana[2]);     
            break;
            case '4':   
                printf("\nDia da semana:%s\nCor da camisa: amarela.\n", diaSemana[3]);      
            break;
            case '5':   
                printf("\nDia da semana:%s\nCor da camisa: vermelha.\n", diaSemana[4]);     
            break;
            case '6':   
                printf("\nDia da semana:%s\nCor da camisa: cinza.\n", diaSemana[5]);        
            break;
            case '7':   
                printf("\nDia da semana:%s\nCor da camisa: rosa.\n", diaSemana[6]);     
            break;
            default:
                printf("Opcao invalida.\n");
            break;
        }
                printf("Deseja fazer uma nova operacao? (s/n)\n");
                fflush(stdin);
                scanf("%c", &opc);
    }
    return 0;
}
  • How are you reading a single character (scanf("%c", &op);) typing 23 he will consider only the 2. Try with 9 or A invalid option will be displayed.

  • has how to make him not consider 23 or any other number like 10, 11 and etc in that case? if not, I appreciate the help anyway.

  • You are reading a single character. If you want to read a string then work with string.

  • One possibility is you read the entire line and check if it has more than one character which is considered an error. If you only have one character your switch/case will identify whether it is valid or not.

  • then means that %c reads only ONE character? if I use %s, what is the difference?

  • I tried to do what you said working with string, but I saw on the internet that it is not possible using switch, only if it was if.. If this is the only way, I prefer not to move. I’m still a layperson in c and I’m trying to learn, thanks

  • With %s you can read a string.

  • Read as string. If the string size > 1 is an error. take the first (and only) character of the string and use it in your switch command.

  • Ending here due to the rules of the site.

  • okay, thank you. :)

Show 5 more comments

1 answer

0


Here is a possibility based on your code, but there are other alternatives.

#include <stdio.h>
#include <string.h>
int main() {
    
    char op[50];
    char opc = 's';
    char *diaSemana[7] = {"\n1. Segunda-feira", "\n2. Terca-feira", "\n3. Quarta-feira", "\n4. Quinta-feira", "\n5. Sexta-feira", "\n6. Sabado", "\n7. Domingo"};
    
    while (opc == 's') {
        
        printf("\n------Menu------\n");
        for (int i = 0; i < 7; ++i) {
            printf("%s\n", diaSemana[i]);
        }

        printf("\nEntre com uma opcao.\n");
        fflush(stdin);
        scanf("%s", op);
        while (strlen(op) > 1) {
            printf("Múltiplos caracteres.\nEntre com uma opcao.\n");
            fflush(stdin);
            scanf("%s", op);
        }
        switch(op[0]) {
            case '1':
                printf("\nDia da semana:%s\nCor da camisa: branca.\n", diaSemana[0]);
            break;
            case '2':
                printf("\nDia da semana:%s\nCor da camisa: verde.\n", diaSemana[1]);
            break;
            case '3':
                printf("\nDia da semana:%s\nCor da camisa: azul.\n", diaSemana[2]);
            break;
            case '4':
                printf("\nDia da semana:%s\nCor da camisa: amarela.\n", diaSemana[3]);
            break;
            case '5':
                printf("\nDia da semana:%s\nCor da camisa: vermelha.\n", diaSemana[4]);
            break;
            case '6':
                printf("\nDia da semana:%s\nCor da camisa: cinza.\n", diaSemana[5]);
            break;
            case '7':
                printf("\nDia da semana:%s\nCor da camisa: rosa.\n", diaSemana[6]);
            break;
            default:
                printf("Opcao invalida.\n");
            break;
        }
        printf("Deseja fazer uma nova operacao? (s/n)\n");
        fflush(stdin);
        scanf("%c", &opc);
    }
    return 0;
}

Browser other questions tagged

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