Having hard time outputing the Band Color from a known value of a resistor
<p>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. </p>

<p>I tried using the log function from <code>&lt;cmath&gt;</code> 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. </p>

<p>Compiler: xcode</p>

<p>language: c++</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;
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 &lt;&lt; "Enter Resistance: ";
    cin &gt;&gt; r;
    cout &lt;&lt; "Enter Tolerance in Decimal Form: ";
    cin &gt;&gt; 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 &lt;&lt; "| Black | ";}
    else if(i == 1) {cout &lt;&lt; "| Brown | ";}
    else if(i == 2) {cout &lt;&lt; "| Red | ";}
    else if(i == 3) {cout &lt;&lt; "| Orange | ";}
    else if(i == 4) {cout &lt;&lt; "| Yellow | ";}
    else if(i == 5) {cout &lt;&lt; "| Green | ";}
    else if(i == 6) {cout &lt;&lt; "| Blue | ";}
    else if(i == 7) {cout &lt;&lt; "| Violet | ";}
    else if(i == 8) {cout &lt;&lt; "| Gray | ";}
    else {cout &lt;&lt; "| White | ";}

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

<p><sub>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.</sub></p>
