The Elektor Forum will close. See also this link. From Friday March 15st it is no longer possible to log in to the forum. However, the content of the forum will remain visible until the end of March. As of April 1st the forum will definitely go off the air.

Anybody could help me with this resistor color code program?

HiFi, LoFi, Tubes, Transistors, ICs, Commercial, DIY

Anybody could help me with this resistor color code program?

Postby Joshua_S » Fri Mar 23, 2018 10:08 am

Hi forum, :D
I've created a code that calculates the resistance of a resistor based on the color combination the user inputs. The code works fine but I feel like my series of else if statements is completely inefficient and I can't seem to find another way the avoid them. Is there anything I could be doing differently while still keeping the struct and still allow the user to input the color as type string?
Here is what I have so far,but I have found it useful: http://www.apogeeweb.net/article/74.html, comprehensively describing What is Resistor and How to Read Resistor Color Code might be worth you a reading.
All right, I know it's a complete mess. Please help me....

Thanks, and have a nice day!

Code: Select all
#include <iostream>
#include<string>
using namespace std;
struct bandLayout
{
   
   
   
    double band1;
   
    double band2;
   
    double band3;
   
    double band4;
   
    double tolerance;
   
    double tempBand;
   
   
};

enum {black, brown, red, orange, yellow, green, blue, violet, grey, white, gold, silver, colorNames};

bandLayout colors[colorNames] =

{
    //black
    { 0,  0,  0, 1 , 0, 0} ,
    //brown
    { 1, 1, 1, 10, .01, 100 },
    //red
    { 2, 2, 2, 100, 0.02, 50 },
    //orange
    { 3, 3, 3, 1000, 0.03, 15 },
    //yellow
    { 4, 4 , 4, 10000, 0.04, 25 },
    //green
    { 5, 5, 5, 100000, 0.05, 0.005 },
    //blue
    { 6, 6, 6, 1000000, 0.0025, 10 },
    //violet
    { 7, 7, 7, 10000000, 0.001, 5 },
    //grey
    { 8, 8, 8, 10e8, 0.0005, 0 },
    //white
    { 9, 9, 9, 0, 0.01, 0 },
    //gold
    { 0, 0, 0, 0, 0.05, 0 },
    //silver
    { 0, 0, 0, 0, 0.1, 0 },
   
};

string bandColor;
int n(0);
int userNum;

int main()
{
   
   
   
   
    double value1, value2, value3, multBand, tempBand;
   
    cout<<"How many bands will you be using? ";
    cin>>userNum;
   
    while(n<userNum)
    {
        cout<<"Band Color"<<endl;
        cin>>bandColor;
        n++;
        if(n==1)
        {
           
            if(bandColor=="brown")
            {
                value1=colors[brown].band1;
            }
           
            else if(bandColor=="red")
            {
                value1=colors[red].band1;
            }
           
            else if(bandColor=="yellow")
            {
                value1=colors[yellow].band1;
            }
           
            else if(bandColor=="green")
            {
                value1=colors[green].band1;
            }
           
            else if(bandColor=="blue")
            {
                value1=colors[blue].band1;
            }
           
            else if(bandColor=="violet")
            {
                value1=colors[violet].band1;
            }
           
            else if(bandColor=="grey")
            {
                value1=colors[grey].band1;
            }
           
            else if(bandColor=="white")
            {
                value1=colors[white].band1;
            }
           
           
        }
       
        if(n==2)
        {
           
            if(bandColor=="brown")
            {
                value2=colors[brown].band2;
            }
           
            else if(bandColor=="red")
            {
                value2=colors[red].band2;
            }
           
            else if(bandColor=="yellow")
            {
                value2=colors[yellow].band2;
            }
           
            else if(bandColor=="green")
            {
                value2=colors[green].band2;
            }
           
            else if(bandColor=="blue")
            {
                value2=colors[blue].band2;
            }
           
            else if(bandColor=="violet")
            {
                value2=colors[violet].band2;
            }
           
            else if(bandColor=="grey")
            {
                value2=colors[grey].band2;
            }
           
            else if(bandColor=="white")
            {
                value2=colors[white].band2;
            }
           
        }
       

        if(n==3)
        {
           
            if(bandColor=="brown")
            {
                multBand=colors[brown].band4;
            }
           
            else if(bandColor=="red")
            {
                multBand=colors[red].band4;
            }
           
            else if(bandColor=="yellow")
            {
                multBand=colors[yellow].band4;
            }
           
            else if(bandColor=="green")
            {
                multBand=colors[green].band4;
            }
           
            else if(bandColor=="blue")
            {
                multBand=colors[blue].band4;
            }
           
            else if(bandColor=="violet")
            {
                multBand=colors[violet].band4;
            }
           
            else if(bandColor=="grey")
            {
                multBand=colors[grey].band4;
            }
           
            else if(bandColor=="white")
            {
                multBand=colors[white].band4;
            }
           
        }
       
       
    }
   
   
    double val1, val2, val3, val4,val5, ohmsValue;
    if(userNum==4)
    {
        val1 = value1*multBand;
        val2 = value2*multBand;
        val3 = val2/10;
        val4 = val1+val3;
        ohmsValue = val4*10;
        cout<<ohmsValue<<" Ohms"<<endl;
        //    toleranceValue = ohmsValue*tolerance;
        //    cout<<ohmsValue<<endl;
        //    cout<<tolerance<<endl;
       
    }
   
    return 0;
}
Edit & Run
Joshua_S
 
Posts: 2
Joined: Fri Mar 23, 2018 9:50 am

Return to Audio & Video

Who is online

Users browsing this forum: No registered users and 1 guest