1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.


Discussion in 'C++' started by rhozeville, Mar 17, 2010.

  1. rhozeville

    rhozeville New Member

    Mar 17, 2010
    Likes Received:
    Trophy Points:
    pls help me make a program that will input the color codes of a resistor and output the resistance using devc++ as a compiler..
  2. xpi0t0s

    xpi0t0s Mentor

    Aug 6, 2004
    Likes Received:
    Trophy Points:
    Senior Support Engineer
    First you need to prompt the user to enter the colours. Does the number of colours vary? If so will you account for this in your program?
    Having entered the colours the program then needs to determine what those colours mean numerically. How you do this will depend on the way you make the user enter the colours: you could be cheeky and in effect make them do the conversion:

    What is the first stripe? Enter 0 for black, 1 for brown, 2 for red (etc)
    What is the second stripe? Enter 0 for black, (etc)


    0: black 1: brown 2: red (etc)
    Please enter the digits for the colours (e.g. for brown black red gold enter 102G):

    (this is also better from a users point of view because if they have a lot of resistors to convert then (a) they will have less typing to do, (b) they will have less frustration because of typos ("sorry, don't recognise "blcak" - please re-enter" *), and (c) this will get them familiar with the numbers so that they won't need to rely on a program)

    * although a neat trick would be to anticipate some possible typos and convert them automatically:
    if (!strcmp(str,"black") || !strcmp(str,"blcak") || !strcmp(str,"blakc"))
    (( Can you think of a way to check for all combinations of b,l,a,c,k without doing 5! strcmps? Because even caklb is distinguishable from nobrw.))

    You will also need to determine if they entered the colours the wrong way. For example gold red black brown is a 1K resistor: they may not know that gold is the last stripe.

    Then you will need to convert the resulting string into a number. Suppose you have "102G", then the first two digits are just converted as digits, and the 3rd digit is the multiplier, so this is (1+0*10)*100=1000 and the last stripe can be converted with a simple switch into the relevant percentage.

Share This Page