Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   devc++ (http://www.go4expert.com/forums/devcpp-t21397/)

rhozeville 17Mar2010 21:57

devc++
 
pls help me make a program that will input the color codes of a resistor and output the resistance using devc++ as a compiler..

xpi0t0s 18Mar2010 14:17

Re: devc++
 
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)

or

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:
Code:

if (!strcmp(str,"black") || !strcmp(str,"blcak") || !strcmp(str,"blakc"))
  code=0;

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


All times are GMT +5.5. The time now is 23:35.