Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
Why my switch case always goes to default case?

I am trying to make a calculator for resistor. So the input is a value in Ohms and the output will be the color band of the resistor.

But I've been stuck in this for a while, I'm not sure what is happening. No matter what, the code always go to the default situation.

char a, b, c; 
int tolerancia,valor;
  
printf("enter resistance value:  ");
scanf("%i", &valor);

c = valor % 10; // th
b = (valor % 100) / 10; // second digit
a = valor / 100; //  first digit

switch (a)                    //colour band for first digit//
{
    case '0':
    printf("black   ");
    break;
    
    case '1':
    printf("brown   ");
    break;
    
    case '2':
    printf("red   ");
    break;
    
    case '3':
    printf("orange   ");
    break;
    
    case '4':
    printf("yellow   ");
    break;
    
    case '5':
    printf("green   ");
    break;
    
    case '6':
    printf("blue   ");
    break;
    
    case '7':
    printf("violet   ");
    break;
    
    case '8':
    printf("grey   ");
    break;
    
    case '9':
    printf("white   ");
    break;
    
    default:
    printf("unknown value   ");
}

The switch case goes for 3 times (first digit, second digit and tolerance) and in every situation the output is "unknown value"

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You're reading an int, but your switch is comparing the first digit of that int to various characters. Now, char is a numeric type, so this (kind of) works, but the value of '0' does not equal 0 and so on for all digit characters.

Thus you go straight to the default case.

More correctly:

switch (a) {
    case 0:
    printf("black   ");
    break;

    // etc.
}

You could also simply have an array of strings and use the digit to index it. Making sure of course to validate that a is a valid index.

char *colors[] = { 
    "black", "brown", "red", "orange", "yellow", 
    "green", "blue", "violet", "grey", "white" 
};

if (a >= 0 && a <= 9) {
    printf("%s   ", colors[a]);
}
else {
    printf("unknown value   ");
}
over 4 years ago · Santiago Trujillo Denunciar

0

Here %i takes an integer value as integer value with decimal, hexadecimal or octal type. To enter a value in hexadecimal format – value should be provided by preceding “0x” and value in octal format. But you are taking the Characters ... Instead of the case '1' : We must write case 1: then it's given correctly.

enter image description here

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda