How many cases can you use on a switch?

Asked

Viewed 84 times

-1

#include <stdio.h>

int main (){
    char sangue, nome[30];
    printf("Informe nome:\n");
    scanf("%s",& nome);
    printf("Informe o sangue de acordo com a tabela\n");
    printf("|--------+-------+|\n");
    printf("| A (x) | A- (w)  |\n");
    printf("|-------+--------+|\n");
    printf("| B (p) | B- (y)  |\n");
    printf("|-------+--------+|\n");
    printf("| AB (t)| AB- (z) |\n");
    printf("|-------+--------+|\n");
    printf("| O (o) | O- (k)  |\n");
    printf("+-------+---------+\n");
    scanf("%s",& sangue);

    switch(sangue){
        case'x':
            printf("Sr(a) %s seu sangue e A+");
            break;

        case'w':
            printf("Sr(a) %s seu sangue e A-");
            break;

        case'p':
            printf("Sr(a) %s seu sangue e B+");
            break;

        case'y':
            printf("Sr(a) %s seu sangue e B-");
            break;

    }
    return 0;
}   
  • Swap scanf("%s",& blood); for scanf("%c",& blood); as it is a single character and not a string. And here: scanf("%s",& name); switch to: scanf("%s", name); as it is a string. This is not the indentation normally used in a switch/case.

2 answers

3


Let’s list the mistakes and some suggestions:

  1. The name does not appear because you are reading a string and saving a char on scanf("%s", &sangue). Read the correct type, changing %s for %c and the variable will be filled correctly.

  2. On the switch you tell the prinft that will print a string (%s) but does not pass any as parameter. The correct would be printf("Sr(a) %s seu sangue e A+", nome);

  3. When reading the person’s name, his code will not accept names with spaces. If the person is called "Maria Laura", only the "maria" will be read. You can solve this using a little regular expression: scanf("%[^\n]%*c", &nome);

  4. Now, as a suggestion, not to keep repeating the text all the time "Sr(a) %s seu sangue é", you can print once before switch and then print only blood type, leaving code more compact.

  5. Finally, I noticed that you avoid using accents in your code. but you can inform the program to use the special Portuguese badges (accents, ç and number formatting through the function setlocale.

Below is an example of implementation with the corrections and suggestions above.

Good studies.

#include <stdio.h>
#include <locale.h>  
//incluímos o header locale.h para utilizar a função setlocale

int main (){
    setlocale(LC_ALL, "pt_BR_utf8"); //daqui pra frente podemos usar acentos
    char sangue, nome[30];
    printf("Informe nome:\n");
    scanf("%[^\n]%*c", &nome);
    printf("Informe o sangue de acordo com a tabela\n");
    printf("|--------+-------+|\n");
    printf("| A (x) | A- (w)  |\n");
    printf("|-------+--------+|\n");
    printf("| B (p) | B- (y)  |\n");
    printf("|-------+--------+|\n");
    printf("| AB (t)| AB- (z) |\n");
    printf("|-------+--------+|\n");
    printf("| O (o) | O- (k)  |\n");
    printf("+-------+---------+\n");

    scanf(" %c", &sangue);      
    printf("Sr(a) %s seu sangue é ", nome); 
    switch(sangue){
        case 'x':
            printf("A+\n");
            break;

        case'w':
            printf("A-\n");
            break;

        case'p':
            printf("B+\n");
            break;

        case'y':
            printf("B-\n");
            break;

        case't':
            printf("AB-\n");
            break;  

        case'z':
            printf("AB-\n");
            break;  

        case'o':
            printf("O\n");
            break;

        case'k':
            printf("K-\n");
            break;
    }
    return 0;
}

1

Lucas, as already commented, your blood variable scanf is incorrect, because you need to catch a char, correcting this, your switch will already work.

However you may have problems because of the scanf with characters, because of this, I put an example of flush in your code, the function flush_in.

#include <stdio.h>

void flush_in() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) { }
}

int main (){
    char sangue, nome[30];

    printf("Informe nome:\n");
    scanf("%s", nome);

    flush_in();

    printf("Informe o sangue de acordo com a tabela\n");
    printf("|--------+-------+|\n");
    printf("| A (x) | A- (w)  |\n");
    printf("|-------+--------+|\n");
    printf("| B (p) | B- (y)  |\n");
    printf("|-------+--------+|\n");
    printf("| AB (t)| AB- (z) |\n");
    printf("|-------+--------+|\n");
    printf("| O (o) | O- (k)  |\n");
    printf("+-------+---------+\n");

    scanf("%c", &sangue);

    switch(sangue){
        case'x':
            printf("Sr(a) %s seu sangue e A+", nome);
            break;

        case'w':
            printf("Sr(a) %s seu sangue e A-", nome);
            break;

        case'p':
            printf("Sr(a) %s seu sangue e B+", nome);
            break;

        case'y':
            printf("Sr(a) %s seu sangue e B-", nome);
            break;

        case't':
            printf("Sr(a) %s seu sangue e AB", nome);
            break;

        case'z':
            printf("Sr(a) %s seu sangue e AB-", nome);
            break;

        case'o':
            printf("Sr(a) %s seu sangue e O", nome);
            break;

        case'k':
            printf("Sr(a) %s seu sangue e O-", nome);
            break;

        default:
            printf("Sr(a) %s, sem sangue encontrado", nome);
            break;
    }

    return 0;
}

See more about the flush, on this question here: Keyboard buffer cleaning after scanf

Browser other questions tagged

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