### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121`` ``````#include 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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include 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.