Resistor color code program

It's the end of the semester and I should be able to understand this, but the logic hasn't come to me at all. I still greatly struggle with knowing what to do. I don't plan on continuing with programming, unless I pick up some books and learn at my own pace.

I was wondering if anyone would be willing to help guide me through the steps on how to do this? Obviously not giving me flat out answers, but helping me along the way.

Here are the instructions:

Using functional decomposition, write a C++ program that will prompt user for the all the color bands of a resistor and then calculate the value of the resistor. The first three bands are digits, the fourth is a power of ten multiplier and the fifth is the tolerance. The color codes can either be defined as a 2-dimensional array of char OR a 1-dimensional array of string. Note: If you type upper case or mixture of upper cases and lower cases, your program should still work.

This is the image associated with the assignment: http://www.chipkin.com/articles/wp-content/uploads/2008/12/resistorcolorchart.jpg

Here is what I have so far. I know it's a complete mess.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    string BAND_COLOR_CODE[10] = { "black", "brown", "red", "orange",
                                   "yellow", "green", "blue", "violet",
                                   "grey", "white" };

    string MULTIPLIER_COLOR_CODE[12] = { "black", "brown", "red", "orange",
                                         "yellow", "green", "blue", "violet",
                                         "grey", "white", "gold", "silver" };

    string TOLERANCE_COLOR_CODE[4] = { "brown", "red", "gold", "silver" };
    
    int bandValues[10]={0,1,2,3,4,5,6,7,8,9};
    int multValues[12]={0,1,2,3,4,5,6,7,8,9,0.1,0.01};
    int tolValues[4]={1,2,5,10};

    string band1,band2,band3;  //band color choices
    string mult,tol;

    getBandColor(BAND_COLOR_CODE,band1,band2,band3);
    //getMultiplyerColor(MULTIPLYER);
    //getToleranceColor(TOLERANCE);

//    cout<<"This resistor has "<<bandColor;
    return 0;
}

void getBandColor(bandColor[],string a,string b,string c)
{
    cout<<"Enter a color for band 1: ";
    cin>>a;
    cout<<"Enter a color for band 2: ";
    cin>>b;
    cout<<"Enter a color for band 3: ";
    cin>>c;
}

//void getMultiplyerColor(string multiply[],co)
//{
 //   string color;
 //   cout<<"Enter a color for the multiplyer: ";
 //   cin>>color;
//}

//void getToleranceColor(string tolerance[])
//{
//    string color;
//    cout<<"Enter a color for the tolerance: ";
//    cin>>color;
//} 



Am I nowhere near the right track?

I think your on the right track.

If you type upper case or mixture of upper cases and lower cases, your program should still work.


IMO the easy way to do this is get the value from the user, then convert it all to upper or lower case.

Line 34
void getBandColor
could be a problem. how are you going to pass 3 values back to main ?

I would put your "cin's" in main and pass that to your functions as lower case.

You can combine line 23 & 24.

Think about how you would do it on paper or a flowchart and it should be easier to concept / program.


Think about how you would do it on paper or a flowchart and it should be easier to concept / program.

Oh, I've tried. I've tried for the last few weeks. The problem is that I can't see in my head what it is I should be doing. I think I'm going to just not turn this assignment in. It's the only thing I haven't turned in, so my grade shouldn't dip that low. Of course, I'll still try until it's time to turn it in..tomorrow. Thank you for your reply.
I tossed together a program to do this a little less than a year ago, IIRC.

It is not the nicest code but perhaps you can make use of it. I have found it useful (I do electronics for fun).
http://www.cplusplus.com/forum/beginner/202882/#msg964976

I don't know if this is required, but while my program follows the rules (at least for the resistors I usually check with it) there is no validation against IEC 60063:1963, so you can compute the supposed resistance of resistors' based on band combinations which don't obey the standard. Which could lead to exciting results like electrical fires.

...Sometimes it's best to check with a meter ;)
Last edited on
Thank you, but that's definitely out of the scope for our class.

I was trying to think, how would I make it so the band names are connected to their int values? From what I'm thinking, I'd have to make switches for them, but instead of doing that three times, is there an easier, less repetitive way?
I used multiple std::maps to associate a color name with a value. As the name suggests, a map <string, int> associates (or "maps") a string "key" to an integer "value".

This is the useful bit of my program. The rest of the program (I linked you to it) does nothing except handle abbreviations and spelling errors.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# include <map>
std::map <std::string, int> const band_value {
  {"black" ,  0}, {"brown", 1}, {"red" , 2}, {"orange",  3},
  {"yellow",  4}, {"green", 5}, {"blue", 6}, {"violet",  7},
  {"grey"  ,  8}, {"white", 9},
};

std::map <std::string, double> const multiplier_value {
  {"black" , 1e0}, {"brown", 1e1}, {"red" ,  1e2}, {"orange",  1e3},
  {"yellow", 1e4}, {"green", 1e5}, {"blue",  1e6}, {"violet",  1e7},
  {"grey"  , 1e8}, {"white", 1e9}, {"gold", 1e-1}, {"silver", 1e-2}
};

/* Temperature coefficient in parts-per-million per degree celsius. */
std::map <std::string, int> const temp_coeff_value {
  {"brown", 100}, {"red" , 50}, {"orange", 15}, {"yellow", 25}
};

/* Resistance tolerance in +/- %. */
std::map <std::string, double> const tolerance_value {
  {"brown",   1.0}, {"red" ,  2.0},  {"green", 0.5}, {"blue",    0.25},
  {"violet",  0.1}, {"grey",  0.05}, {"gold",  5.0}, {"silver", 10.0},
  {"none",   20.0}
};

std::vector <std::string> const colors {
  "black" , "brown", "red"  , "orange", "yellow", "green", "blue",
  "violet", "grey" , "white", "silver", "gold"
};

void print_resistor (std::vector <std::string> bands) {
  auto compute_ohms = [](auto start_iter, auto end_iter) -> double {
    double ohm_result = 0.0;
    while (start_iter != end_iter) {
      /* Make sure we're not looking at the multiplier band */
      if (start_iter != (end_iter - 1)) {
        ohm_result *= 10; /* decimal ALSH (e.g., 14 -> 140) */
        ohm_result += band_value.at(*start_iter++);
      } else { /* Looking at last band. */
        return ohm_result * multiplier_value.at(*start_iter);
      }
    }

    return ohm_result;
  };

  double ohm_result, tolerance, temp_coeff;
  ohm_result = tolerance = temp_coeff =
    std::numeric_limits<double>::infinity();

  try {
    switch (bands.size()) {
    case 3: {
      ohm_result = compute_ohms(bands.begin(), bands.end());
      tolerance  = tolerance_value.at("none");
    } break;

    case 4:
    case 5: {
      ohm_result = compute_ohms(bands.begin(), bands.end() - 1);
      tolerance  = tolerance_value.at(bands.back());
    } break;

    case 6: {
      ohm_result = compute_ohms(bands.begin(), bands.end() - 2);
      tolerance  = tolerance_value.at(*(bands.end () - 2));
      temp_coeff = temp_coeff_value.at(bands.back());
    } break;

    default:
      std::cerr << "Incorrect number of bands. \n";
      std::exit(1);
    }
  } catch (std::out_of_range const e) {
    std::cerr << "Unrecognized band type.  Check your resistor!\n";
    std::exit(1);
  }

  std::cout << ohm_result << " ohms at " << tolerance << " percent tolerance";

  if (! std::isinf(temp_coeff)) {
    std::cout <<", temperature coeff. " << temp_coeff << "\n";
  } else std::cout << ".\n";
}


My switch-case is on the number of bands on the resistor -- for instance, a 6-band resistor has 4 bands of color, one tolerance band, and a temperature coefficient band. This way I know which bands are which.

The input to print_resistor is a string containing the color-names of the bands. For example, a vector containing {"red", "red", "blue"}.


Last edited on
What is a map? We haven't used that. This is a purely beginner class, we barely scratched the surface.
A map is a container that associates (or "maps") keys of some type to a single corresponding value of some type.

You can replace them with parallel arrays, or lists of pairs. Are you familiar with vectors (which are easier-to-use, safer arrays of dynamic size)?
Last edited on
Oh, ok. Yeah, I haven't even heard of maps before and we haven't done vectors.

I was thinking of using parallel arrays, but my textbook covers it in a page. It doesn't explain much. I'm just confused how to set things up. I need the name to show, but not the value. I'll keep working on it.
How would I make it so each int value correlates to its respective color name using parallel arrays? I believe I have everything down except for that. My calculation shows zero for both the ohms and tolerance.
Can anyone help me with that one thing? I've got everything else. I can't figure out how to convert the string to an int.
Use two arrays of equal length:
1
2
string colors[n_colors] = {"black", "brown", "red", ... }; 
int band_values[] = {0, 1, 2, ...};

Loop through "colors", looking for the matching string. For example:
1
2
3
4
5
6
int get band_value(string color) {
  for (int i = 0; i < n_colors; ++i) 
    if (input_string == colors[i]) return band_values[i];

  return 0; // some default
}


You can write another functions to convert a color band to the corresponding multiplier exponent, for instance.
Last edited on
I already had my two arrays. Both are in main and I passed my string array as an argument. So I should have both in my function?
Topic archived. No new replies allowed.