coding for to make resistors values into colors

in devc++ have been stuck for a solid 6 days on trying to figure out how to enter resistor value and how to get the colors back. I had to write a class for resistors. I have added my function for my first band and one of my functions in the main im trying to call. Please help me.
1. how do i get the function to read just the first and second value, then how do i count the leading zeros to get the color of the third resistor?
Thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const string resistance::band_a(int x)
			{	 string color;
				
				if(x == 1)
					color = band_a[0];
				else if(x == 2)
					color = band_a[1];
				else if(x == 3)
					color = band_a[2];
				else if(x == 4)
					color = band_a[3];
				else if(x == 5)
					color = band_a[4];
				else if(x == 6)
					color = band_a[5];
				else if(x == 7)
					color = band_a[6];
				else if(x == 8)
					color = band_a[7];
				else if(x == 9)
					color = band_a[8];
				else 
					cout << "Please enter a valid number from 1-9" << endl; 
				return color;

this is in my main

cout << "Please enter your resistor number without tolerance" << endl;
cin >> num;
if(num > 0.1 && num < 100000000)
	while(num >=.1)
	aaa = num*10;					{
cout << band1.band_a(aaa);
							}
						}
Last edited on
My code is quite ugly, but I wrote a program like this a while ago.
Perhaps it will be useful to you?
https://gist.github.com/mbozzi/034e963a91c23a1b41424e7d824afaa7
Assumes a 4-band resistor, without the tolerance band.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

string resistor( double ohms )
{
   const string colour[] = { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" };

   int multiplier = floor( log10( ohms ) - 1.0 );          // power of 10 after first two digits; may be negative
   int digits = ohms / pow( 10.0, multiplier ) + 0.5;      // should round down to first two digits
   int first  = digits / 10;
   int second = digits % 10;

   string bands = colour[first] + " " + colour[second] + " ";

   if ( multiplier < -2 || multiplier > 9 ) bands += "problems!";
   else if ( multiplier == -2 ) bands += "silver";
   else if ( multiplier == -1 ) bands += "gold";
   else                         bands += colour[multiplier];

   return bands;
}

int main()
{
   double ohms;
   cout << "Input resistance in ohms ( 0.1 < R < 1e9 ): ";   cin >> ohms;
   cout << resistor( ohms );
}


Input resistance in ohms ( 0.1 < R < 1e9 ): 47000
yellow violet orange

Input resistance in ohms ( 0.1 < R < 1e9 ): 5.2
green red gold
Last edited on
Topic archived. No new replies allowed.