Need help troubleshooting resistor calculator c++

Hello World!

I have written up a program that asks a user to input the four bands from a resistor, and it should determine the resistors value and tolerance from the four colours. The program runs but the math doesn't seem to work out, nor the tolerance. I am looking for help troubleshooting the program.

#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
string band[4];
string colour[12]{"black","brown","red","orange","yellow","green","blue","violet","grey","white","gold","silver"};
int band_value[12];
int resistor;
int i;
int band_number[4];


for (i=0;i<4;i++);
{
cout<<"what are the four colours of your resistor\n"<<i+1;
cin>>band[i];
}

for (i=0;i<4;i++)
{
if (band[i]=="black")
{
band_number[i]=0;
}
if (band[i]=="brown")
{
band_number[i]=1;
}
if (band[i]=="red")
{
band_number[i]=2;
}
if (band[i]=="orange")
{
band_number[i]=3;
}
if (band[i]=="yellow")
{
band_number[i]=4;
}
if (band[i]=="green")
{
band_number[i]=5;
}
if (band[i]=="blue")
{
band_number[i]=6;
}
if (band[i]=="violet")
{
band_number[i]=7;
}
if (band[i]=="grey")
{
band_number[i]=8;
}
if (band[i]=="white")
{
band_number[i]=9;
}
if (band[i]=="silver")
{
band_number[i]=10;
}
if (band[i]=="gold")
{
band_number[i]=5;
}
}

resistor=((band_number[0]*10)+band_number[1])*pow(10, (band_number[2]));

cout<<resistor<<"ohms "<<"+/-"<<band_number[3]<<"%";

return 0;

}
The meaning of each band depends on the number of bands.
See this thread, for example:
http://www.cplusplus.com/forum/beginner/204169/#msg969288

If you can assume there will always be four bands, then the first 2 bands are value, the third band is the multiplier, and the fourth band is the tolerance. This makes the program simpler. For instance, write three functions:
1
2
3
int to_value(string value_band_color); 
int to_multiplier_exponent(string multiplier_band_color);
int to_tolerance_percent(string resistance_band_color);

Note that different colors mean different things in

And then compute the resistance:
1
2
int resistance = (to_value(color[0]) * 10 + to_value(color[1])) *
  pow(10, to_multiplier_exponent(color[2]));

And then get the tolerance:
int tolerance = to_tolerance_percent(color[3]);
Last edited on
Topic archived. No new replies allowed.