Switch command running even without being called - C

Asked

Viewed 27 times

1

I’m solving an exercise in logic and I can’t understand the error:

#include <stdio.h>
#include <stdlib.h>

int main(){
int TotalVinhos=0, t=0, b=0, r=0, fim = 0;
//float Porc;
char tipo;

while(fim == 0){
  printf("Tipo do vinho: ");
  scanf("%c", &tipo);

switch(tipo){
    case 'T':
        t++;
        TotalVinhos++;
        break;
    case 'B':
        b++;
        TotalVinhos++;
        break;
    case 'R':
        r++;
        TotalVinhos++;
        break;
    case 'F':
        fim = 1;
        break;
    default:
        printf("erro..");
        break;
  }

}

 printf("Total de vinhos: %d\n", TotalVinhos);
 printf("Quantida de vinho tipo T: %d\n", t);
 printf("Quantida de vinho tipo B: %d\n", b);
 printf("Quantida de vinho tipo R: %d", r); 
}

The error presented to me:

Ele executa 2x e já da a opção default

  • This code does not even compile, at least in a decent compiler. Even this code cannot give the result shown. If you put one code and are using another it becomes complicated to help.

  • Which compiler is decent?

  • @bigown see, I changed the code.. now it’s to compile but keep the error there

  • It got worse. You have to post something that you know is at least compiling.

  • I fixed the errors, gave an organized, but I kept the essence of the code and is working. http://ideone.com/ohtaSe

  • I tested the code I just got on repl.it and ran but with the bug

  • The code you posted is also with the same error:

  • 1

    Not for me https://i.stack.Imgur.com/lX9ri.png

Show 3 more comments

1 answer

1


An array of char ends with \0 then created with size 2 (one for the character and one for the \0). When passing the value to the case, I passed only the type[0], which is the necessary value. I also changed the bool because there was no include for type bool in the code. Replaces by int with 1 and 0 that works in the same way.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int TotalVinhos=0, t=0, b=0, r=0;
    //float Porc;
    int fim=1;
    char tipo[2];

    do
    {
        printf("Tipo do vinho: ");
        scanf("%s", &tipo);

        switch(tipo[0])
        {
        case 'T':
            t++;
            TotalVinhos++;
            break;
        case 'B':
            b++;
            TotalVinhos++;
            break;
        case 'R':
            r++;
            TotalVinhos++;
            break;
        case 'F':
            fim = 0;
            break;
        default:
            printf("erro..");
            break;
        }

    }
    while(fim);

    printf("Total de vinhos: %d\n", TotalVinhos);
    printf("Quantida de vinho tipo T: %d\n", t);
    printf("Quantida de vinho tipo B: %d\n", b);
    printf("Quantida de vinho tipo R: %d", r);
}

I don’t know if there’s any other way to do it, but so it’s working properly.

  • Well it worked, I just don’t understand why on the switch you put "like[0]"

  • 1

    Because it is the position that is the character you received, ( the other "type[1]" is with the " 0"). I’ve already edited the answer with a short explanation of what was changed.

  • For example, if I received a’T' in the scanf, the type variable will be: type = [’T', '/0']

Browser other questions tagged

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