Switch command running even without being called - C

1

I am solving a logic exercise and I can not 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 that is presented to me:

    
asked by anonymous 13.05.2017 / 21:45

1 answer

1

An array of char ends with bool so I created it with size 2 (one for the character and one for the bool ). At the time of passing the value to the case, I only passed the type [0], which is the value needed. I also changed %code% because there was no include for type %code% in code. Replaces with int with 1 and 0 which works 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 do not know if there is any other way to do it, but this is working correctly.

    
13.05.2017 / 22:21