From my experiences so far on this forum, I've noticed that engineers seem to have the hardest part starting with programming, not just C++, but all languages. It must be something to do with the fact that numbers is all they really use (please don't take that as an insult). But nonetheless, we all need help sooner or later.
I don't typically do this, but I decided to sit down and take a pretty quick swing at your problem. This is poorly implemented and even more so commented. I hope you can follow along:
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>
void EnterBands(int numOfBands, int bands[]);
void DisplayResistance(int numOfBands, int bands[]);
int main() {
int numOfBands;
int bands[6];
std::cout << "Please enter the number of bands on the resistor (4 - 6):";
// There is no error testing done with choice, but should be implemented
std::cin >> numOfBands;
switch (numOfBands) {
case 4: ///< 4 Band Resistor
EnterBands(numOfBands, bands);
break;
case 5: ///< 5 Band Resistor
break;
case 6: ///< 6 Band Resistor
break;
}
DisplayResistance(numOfBands, bands);
return 0;
}
void EnterBands(int numOfBands, int bands[]) {
for (int i = 0; i < numOfBands; i ++) {
std::cout << "\nPlease enter your band #" << i + 1 << ":";
// Can convert from colors later on
std::cin >> bands[i];
}
}
// This function is used to convert the bands into resistances
void DisplayResistance(int numOfBands, int bands[]) {
long resistance = 0;
// Calculates the bands up to the multiplier
for (int i = 0; i < numOfBands - 2; i ++) {
resistance *= 10;
resistance += bands[i];
}
// Calcualtes the multiplier
for (int i = 0; i < bands[numOfBands - 2]; i ++)
resistance *= 10;
std::cout << "Your resistor is rated at:\n";
std::cout << "\tMin:" << resistance -
(resistance * bands[numOfBands - 1] / 100) << "\n";
std::cout << "\tMax:" << resistance +
(resistance * bands[numOfBands - 1] / 100) << "\n";
}
|
Now, I want you to know, I learned about resistors about 10 years ago, and since, I have completely forgotten how they're marked, I just knew it involved colors. After a search or two on wikipedia, I stumbled across a way to calculate the values, and a really brief example.
Wikipedia wrote: |
---|
For example, a resistor with bands of yellow, violet, red, and gold will have first digit 4 (yellow in table below), second digit 7 (violet), followed by 2 (red) zeros: 4,700 ohms. Gold signifies that the tolerance is ±5%, so the real resistance could lie anywhere between 4,465 and 4,935 ohms. |
Here is the above output:
Please enter the number of bands on the resistor (4 - 6):4
Please enter your band #1:4
Please enter your band #2:7
Please enter your band #3:2
Please enter your band #4:5
Your resistor is rated at:
Min:4465
Max:4935 |
I hope this gets you a few steps closer.
Note: I didn't implement the color's mainly because that would have taken a while, but you can at least see the basic outline of the program. All bands, minus the tolerance, are going to be a set value, so you can have that in one function, get the value of the tolerance in another function, or the same if you're brave, and you'll end up just reusing the code for Display Resistance.