Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a simple program to convert a Resistance Value to it's corresponding color bands. I'm trying to think of the most efficient way that will be universal for 4,5 and 6 Band Resistors. Inputting the color and calculating the resistance value is no problem. The other way around is really giving me a headache.

I tried using the log function from <cmath> library with a 4 band and succeeded. However, I cannot think of how to use it for 5 and 6 band resistors. Is there a way and if so is there a more efficient way that I haven't thought of yet. Below is my code using the log function built for a 4 band resistor.

Compiler: xcode

language: c++

#include <iostream>
#include <cmath>
using namespace std;
void band3_to_color(int);
void tolerance_to_color(float);
int main()
{
    float r = 0.0f;
    float t = 0.0f;
    int exp = 0;
    cout << "Enter Resistance: ";
    cin >> r;
    cout << "Enter Tolerance in Decimal Form: ";
    cin >> t;
    exp = log10( r / 10.f );
    band3_to_color(r / pow(10,exp+1));
    band3_to_color( (int( r / pow(10,exp ))) % 10);
    band3_to_color(exp);
    tolerance_to_color(t);
    return 0;
}
void band3_to_color(int i){

    if(i == 0) {cout << "| Black | ";}
    else if(i == 1) {cout << "| Brown | ";}
    else if(i == 2) {cout << "| Red | ";}
    else if(i == 3) {cout << "| Orange | ";}
    else if(i == 4) {cout << "| Yellow | ";}
    else if(i == 5) {cout << "| Green | ";}
    else if(i == 6) {cout << "| Blue | ";}
    else if(i == 7) {cout << "| Violet | ";}
    else if(i == 8) {cout << "| Gray | ";}
    else {cout << "| White | ";}

}
void tolerance_to_color(float i){
    if (i == .1){cout << "| Silver | ";}
    else if(i == .05){cout << "| Gold | ";}
    else if(i == .01){cout << "| Brown | ";}
    else if(i == .02){cout << "| Red | ";}
    else if(i == .005){cout << "| Green | ";}
    else if(i == .0025){cout << "| Blue | ";}
    else {cout << "| Purple | ";}
}

Note the above was actually in a switch under a class. So I'm familiar with switch. I'm just trying to problem solve my formula or approach in making my log function work with 5 and 6 band.

share|improve this question
4  
You really need to learn about switch... –  Florian Margaine Mar 31 '13 at 2:20
    
The above was actually in a switch under a class. So i'm familiar with switch. I'm just trying to problem solve my formula or approach in making my log function work with 5 and 6 band. –  Shane Yost Mar 31 '13 at 2:25
add comment

closed as too localized by MainMa, thorsten müller, Martijn Pieters, Kilian Foth, MichaelT Apr 1 '13 at 2:32

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 4 down vote accepted

I cannot think of how to use it for 5 and 6 band resistors.

Resistance values for 5- and 6-band resistors are specified in exactly the same way as 4-band resistors:

  • The first three bands indicate a 3-digit resistance value.

  • The fourth band indicates log10 of the multiplier.

  • The 5th band, if present, specifies the tolerance.

  • The 6th band, if present, specifies the temperature coefficient.

So, the user will have to specify more than just the resistance value if they want a 5- or 6-band response. You say that you're familiar with switch, so it shouldn't be a problem to simply switch on the additional input(s) to arrive at the extra band colors. In fact, you might want to use a series of if statements instead so that you can provide an appropriate output even if the user enters a non-standard value for the tolerance and temperature coefficient, like:

if (tolerance <= 0.05) { color = gray; }
else if (tolerance <= 0.1) { color = violet; }
else if (tolerance <= 0.25) { color = blue; }
...
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.