Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!