# Resistor value to color code and vice versa

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-29-2012
Eddie Ramos
Resistor value to color code and vice versa
I'd like to thank you all for your time and help in advanced much appreciated.

Microsoft Visual Studio 2012
C++ Console Application

What my program is to do:
-ask user for value ex. 200 ohms
- output the value in color ex brown black brown
and vice versa
-ask user for color ex brown black brown
-output the value ex 200 ohms

a few background summary:
I'm very new to C++(2 days) i have self taught myself c++ enough to make my very first working program. i got it to output the value based on the color numbers entered. basically am trying to make a resistor color code calculator.

My problem:
- it only outputs the value based on color numbers
how do i make it output value in colors?
- is there better ways of doing this?

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {     double colornum,multiplier;     cout<<"0 = black"<<endl;     cout<<"1 = brown"<<endl;     cout<<"2 = red"<<endl;     cout<<"3 = orange"<<endl;     cout<<"4 = yellow"<<endl;     cout<<"5 = green"<<endl;     cout<<"6 = blue"<<endl;     cout<<"7 = violet"<<endl;     cout<<"8 = grey"<<endl;     cout<<"9 = white" <<endl<<endl;     cout<<"Enter 1st & 2nd band color numbers: ";     cin>>colornum;     cout<<"Enter 3rd band color number(Multiplier): ";     cin>>multiplier;     cout <<"The color code of a "<<colornum<<multiplier<< " resistor is: ";     if ( multiplier == 0) {         multiplier = colornum * pow( 10,0 );     } else if ( multiplier == 1 ) {         multiplier = colornum * pow( 10,1 );     } else if ( multiplier == 2 ) {         multiplier = colornum * pow( 10,2 );     } else if ( multiplier == 3 ) {         multiplier = colornum * pow( 10,3 );     } else if ( multiplier == 4 ) {         multiplier = colornum * pow( 10,4 );     } else if ( multiplier == 5 ) {         multiplier = colornum * pow( 10,5 );     } else if ( multiplier == 6 ) {         multiplier = colornum * pow( 10,6 );     } else if ( multiplier == 7 ) {         multiplier = colornum * pow( 10,7 );     } else if ( multiplier == 8 ) {         multiplier = colornum * pow( 10,8 );     } else {         multiplier = colornum * pow( 10,9 );     }     cout <<multiplier<<endl;     return 0; }```
• 09-29-2012
std10093
Notice that
Code:

`cout <<"The color code of a "<<colornum<<multiplier<< " resistor is: ";`
will print the value of colornum and multiplier with no space between them.For example if colornum=2 and multiplier=5 it will output The color code of a 25 resistor is:

As for your problem ,i do not understand what you mean :/
• 09-29-2012
Eddie Ramos
am trying to make my program output the colors rather then the numbers

resister references:
http://www.dannyg.com/examples/res2/resistor.htm
Electronic color code - Wikipedia, the free encyclopedia

the basic calculation is

color1color2 x 10^multiplier

12 x 10^2 = 1000 ohms (1k ohms) color = brown black red
1 being brown
2 being red
multiplier being red which is 2
• 09-29-2012
std10093
I cannot load both pages for some reason(mine problem i would say).

If i input 0 2 i get the output
Code:

`Enter 1st & 2nd band color numbers: Enter 3rd band color number(Multiplier): The color code of a 02 resistor is: 0`
What do you want the output to be?Just give me an example please :)
• 09-29-2012
Eddie Ramos
the first input asked the user for 2 values from the color code number list no spaces btw all stuck together

cout<<"0 = black"<<endl;
cout<<"1 = brown"<<endl;
cout<<"2 = red"<<endl;
cout<<"3 = orange"<<endl;
cout<<"4 = yellow"<<endl;
cout<<"5 = green"<<endl;
cout<<"6 = blue"<<endl;
cout<<"7 = violet"<<endl;
cout<<"8 = grey"<<endl;
cout<<"9 = white" <<endl<<endl;

Say i enter 10 for brown and black

then it will ask the user for a third band color

say i enter 2 for red

output is 102 which should be brown black red

i want the output to be the name of the color relating to the numbers the user entered.

OUTPUT:
a 102 resistor is brown black red
• 09-29-2012
std10093
Sorry for my difficulty to understand .

So what you want to do is to isolate every digit of the result and then make an if-else structure in order to print the colors.

I made an example (in c by accident.If this bothers you tell me to convert it to c++).The example has more things that you need,because i wanted to show you how the mod and division are used to extract the digits of a number.Surely this algorithm can be way better.

Code:

```#include <stdio.h> int main(void) {         int n=152;         int a,b,c,d,f,g,z;         a=n/100;         b=n/10;         c=n/1;         printf("a= %d  b = %d  c=%d\n",a,b,c);         d=n%100;         f=n%10;         g=n%1;         printf("with mod->d= %d  f = %d  g=%d\n",d,f,g);         z=b%10;         printf("z=%d\n",z);                 if(a==0)         {                 printf("black\t");         }         else if(a==1)         {                 printf("brown\t");         }         ...                 return 0; }```
• 09-29-2012
Eddie Ramos
Np my problem is actually pretty hard to explain and understand as well but yes that's exactly what i want to do :)
my compiler can't run C can you pleas convert it to c++ if its not too much trouble.
• 09-29-2012
std10093
Code:

```#include <iostream> using namespace std;   int main(void) {     int n=152;     int a,b,c,d,f,g,z;     a=n/100;     b=n/10;     c=n/1;     cout<<"a= "<< a <<"  b = "<< b <<"  c= " << c <<endl;     d=n%100;     f=n%10;     g=n%1;     cout<<"with mod->d= " << d <<"  f = "<< f <<" g= "<< g <<endl;     z=b%10;     cout<<"z = "<< z <<endl;         if(a==0)     {         cout<<"black    ";     }     else if(a==1)     {         cout<<"brown    ";     }     ...         return 0; }```
hope this helps :)
• 09-30-2012
Aslaville
Could you help me understand this please,what do you mean by "pow()" in your code?
• 09-30-2012
std10093
Quote:

Originally Posted by Aslaville
Could you help me understand this please,what do you mean by "pow()" in your code?

It is a function :)

pow - C++ Reference
• 09-30-2012
Aslaville
Is that code compiling in your compiler?Mine is developing some crazy things.Am on Microsoft Visual 2010
• 09-30-2012
std10093
Quote:

Originally Posted by Aslaville
Is that code compiling in your compiler?

If you are referring to the code of post #1,yes it compiled :)

What errors do you get?
• 09-30-2012
Aslaville
It simply says that the 'pow' identifier is undefined
• 09-30-2012
std10093
Have you included this library <cmath> ? :)
• 09-30-2012
Aslaville
yes of course.I do not know what is wrong with the compiler.It is developing about six hundred lines of different code when I compile and try to get the error from the output section but the same code compiled on Code::blocks.I did not however know of the function prior to this conversation.Thanks for the update.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last