Help!! Input Resistor Value to Determine Resistors Band Colors


I've successfully build a program to read in colors and read out the values for 4 band, 5 band and 6 band resistors using classes. Now I need to do the opposite which is input the value and write the code to display it's colors.

I'm really having a tough time thinking about this. I've spent a solid 4 hours already thinking about a strategy in doing this. I can think of ways to do it for just a 4 Band or just a 5 Band and so on. However I can't seem to find a efficient way to make it universal for all types of resistors (4,5,6 band resistors).

Without giving me the complete answer (source code) can someone help me out? A hint or just a small shove into the right direction would be appreciated.
Tested and working. I could have done a better job at the colour/multiplier/tolerance/temprature lookup. The trick is using the log10 function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cmath>

enum colour {
    black,
    brown,
    red,
    orange,
    yellow,
    green,
    blue,
    violet,
    gray,
    white,
    gold,
    silver,
    none
};

class Resistor {
    int colour2int(colour c) {
        switch(c) {
        case black:  return 0;
        case brown:  return 1;
        case red:    return 2;
        case orange: return 3;
        case yellow: return 4;
        case green:  return 5;
        case blue:   return 6;
        case violet: return 7;
        case gray:   return 8;
        case white:  return 9;
        case gold:   return -1;
        case silver: return -2;
        default:     return 0;
        }
    }

    colour int2colour(int i) {
        switch(i) {
        case -2: return silver;
        case -1: return gold;
        case  0: return black;
        case  1: return brown;
        case  2: return red;
        case  3: return orange;
        case  4: return yellow;
        case  5: return green;
        case  6: return blue;
        case  7: return violet;
        case  8: return gray;
        case  9: return white;
        default: return none;
        }
    }

    float colour2tolerance(colour c4) {
        switch (c4) {
        case brown : return 0.01f;
        case red   : return 0.02f;
        case yellow: return 0.05f;
        case green : return 0.005f;
        case blue  : return 0.0025f;
        case violet: return 0.001f;
        case gray  : return 0.0005f;
        case gold  : return 0.05f;
        case silver: return 0.10f;
        case none  :
        default    : return 0.20f;
        }
    }

    float colour2temp(colour c5) {
        switch (c5) {
        case black : return 250.f;
        case brown : return 100.f;
        case red   : return  50.f;
        case orange: return  15.f;
        case yellow: return  25.f;
        case green : return  20.f;
        case blue  : return  10.f;
        case violet: return   5.f;
        case gray  : return   1.f;
        default    : return   0.f;
        }
    }

    float colour2resistance(colour c1, colour c2, colour c3) {
        return (colour2int(C1)*10 + colour2int(C2)) * pow(10.f , (float)colour2int(C3));
    }

public:
    Resistor() : C1(none), C2(none), C3(none), C4(none), C5(none), R(0.f) {}

    colour C1;  // The colour bands
    colour C2;
    colour C3;
    colour C4;
    colour C5;
    float R;    // The resistance value
    float Tol;  // The tolerance
    float TC;   // Temperature Coeffecient

    void setColours(colour c1, colour c2, colour c3, colour c4 = none, colour c5 = none) {
        C1  = c1;
        C2  = c2;
        C3  = c3;
        C4  = c4;
        C5  = c5;
        R   = colour2resistance(c1,c2,c3);
        Tol = colour2tolerance(c4);
        TC  = colour2temp(c5);
    }

    void setResistance(float r) {
        int exp = (int) std::log10( r / 10.f );
        R  = r;
        C3 = int2colour(exp);
        C1 = int2colour( (int) R / pow(10,exp+1) );
        C2 = int2colour( (int( R / pow(10,exp  ))) % 10 );
    }
};
Last edited on
I did this for the 4 band resistor so now how do I make it universal for the 5 and 6. I am having a hard time understanding your code. I only want to go from value to color. You said it relies on where you place the log. Could you elaborate more on what I should do next by the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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 | ";}
}


Here is the output

Enter Resistance: 25000
Enter Tolerance in Decimal Form: .05
| Red | | Green | | Orange | | Purple |
Last edited on
Oh, I thought you said that you needed to use classes in your original post. That's why I made a class.

In the 5 band resistor, the code I gave you does this:
1st band, resistance (1st digit).
2nd band, resistance (2nd digit)
3rd band, resistance multiplier
4th band, tolerance
5th band, temperature coeffecient.

In the 6 band resistor, the code is like this:
1st band, resistance (1st digit).
2nd band, resistance (2nd digit)
3nd band, resistance (3rd digit)
4th band, resistance multiplier
5th band, tolerance
6th band, temperature coeffecient.

You can use ALMOST the same algorithm. Just play with exp = log10 ( r / 100.f)
Topic archived. No new replies allowed.