## Tinee

Hi,

I need help finding the problem with my program. I believe everything is working but when I input the the tolerance, the program suddenly terminates. Please help me spot the problem. By the way, as a warning my program is very long but redundant. hehe.

``````#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cmath>
#include <iomanip>

using namespace std;
//------------------Function Prototype---------------------
int ConverterA(string);
int ConverterB(string);
int ConverterD(string);
int Converter(string);
string ConverterD(int);
string NumberConv (char);
//------------------Class----------------------------------
class Resistance
{
private:
string ColorA[9], ColorB[10], ColorC[12], ColorD[12];
double Resistances, Tolerance;

public:
double getResistance();
double getTolerance();
int Option();
double Rmax(double, double);
double Rmin(double, double);
double TolConv(string);
double R(double, double);
};

double Resistance:: getResistance()
{

do{
cout << "\nPlease enter the desired resistance \nvalue in ohms (0.01 ohms to 100,000,000 ohms): ";
cin >> Resistances;

if (Resistances <= 0.01 && Resistances >= 100000000)
{
cout << "Invalid Input! Would you like to try again? Enter Y: " ;
}
}

return Resistances;
}

double Resistance:: getTolerance()
{
do{
cout << "\nPlease enter the desired tolerance as a percentage(5, 10, or 20): ";
cin >> Tolerance;

if (Tolerance != 5 && Tolerance != 10 && Tolerance != 20)
{
cout << "Invalid Input! Would you like to try again? Enter Y: " ;
}
}

}

double Resistance::Rmax(double resist, double tol)
{
resist = (resist*(tol/100.0))+resist;
return resist;
}
double Resistance::Rmin(double resist, double tol)
{
resist = resist-(resist*(tol/100.0));
return resist;
}

double Resistance::TolConv(string color)
{
return ConverterD(color);
}

double Resistance::R(double resist, double color)
{
resist = resist*pow(10.0,color);
return resist;
}

int Resistance:: Option ()
{
int option;

cout << "Please Choose From The Fallowing Option : " << endl;
cout << "\t1 - Convert color of bands to ohms\n\t2 - Convert ohm to color of bands" << endl;
cout << "Enter Option Number : " ;
cin  >> option;
cout << endl << endl ;
return option;

}
int main ()
{
Resistance A;
double resist, tol, bandAnB, bandC, Cnum;
string bandD, countAnB, anb;

cout << "Is the ohm a decimal? Enter Y : " ;
cin >> deci;

if (deci == 'y' || deci =='Y')
{
cout << "\nPlease enter 0 first then the decimal point. (Ex. 0.1) " ;
resist = A.getResistance();
tol = A.getTolerance();

bandD = ConverterD(tol);

itoa(resist,AnB,16);
countAnB = AnB;

bandD = ConverterD(tol);
bandC = countAnB.length()-2;

itoa(bandC,C,16);

anb = countAnB.substr(2,2);

double result;
stringstream convert(anb);
if(!(convert >> result))
result = 0;

cout << "\nResults:" << endl;

do
{

if(tol == 5)
{

if( result <= 10 )
{
cout << "Closest Standard Value = 0.10" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : black" << endl;
}

else if( result > 10 && result <=11 )
{
cout << "Closest Standard Value = 0.11" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : brown" << endl;
}
else  if( result > 11 && result <=12 )
{
cout << "Closest Standard Value = 0.12" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : red" << endl;
}
else  if(  result > 13 && result <=15 )
{
cout << "Closest Standard Value = 0.13" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : green" << endl;
}
else  if(  result > 15 && result <=16  )
{
cout << "Closest Standard Value = 0.15" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : blue" << endl;
}
else  if(  result > 16 && result <=18  )
{
cout << "Closest Standard Value = 0.16" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : grey" << endl;
}
else  if(  result > 18 && result <=20  )
{
cout << "Closest Standard Value = 0.18" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : black" << endl;
}
else  if(  result > 20 && result <= 22  )
{
cout << "Closest Standard Value = 0.20" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : red" << endl;
}
else  if(  result > 22 && result <= 24  )
{
cout << "Closest Standard Value = 0.22" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : yellow" << endl;
}
else  if( result > 24 && result <=27 )
{
cout << "Closest Standard Value = 0.24" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : violet" << endl;
}
else  if(  result > 27 && result <= 30  )
{
cout << "Closest Standard Value = 0.27" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : black" << endl;
}
else  if(  result > 30 && result <= 33  )
{
cout << "Closest Standard Value = 0.30" << endl;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : orange" << endl;
}
else  if(  result > 33 && result <=36  )
{
cout << "Closest Standard Value = 0.33" << endl;;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : blue" << endl;
}
else  if(  result > 36 && result <= 39  )
{
cout << "Closest Standard Value = 0.36" << endl;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : white" << endl;
}
else   if(  result > 39 && result <= 43  )
{
cout << "Closest Standard Value = 0.39" << endl;
cout << "Color Band A : yellow" << endl;
cout << "Color Band B : orange" << endl;
}
else   if(  result > 43 && result <= 47  )
{
cout << "Closest Standard Value = 0.43" << endl;
cout << "Color Band A : yellow" << endl;
cout << "Color Band B : violet" << endl;
}
else   if(  result > 47 && result <= 51  )
{
cout << "Closest Standard Value = 0.47" << endl;
cout << "Color Band A : green" << endl;
cout << "Color Band B : brown" << endl;
}
else   if(  result > 51 && result <= 56  )
{
cout << "Closest Standard Value = 0.51" << endl;
cout << "Color Band A : green" << endl;
cout << "Color Band B : blue" << endl;
}
else    if(  result > 56 && result <= 62  )
{
cout << "Closest Standard Value = 0.56" << endl;
cout << "Color Band A : blue" << endl;
cout << "Color Band B : red" << endl;
}
else    if(  result > 62 && result <= 68  )
{
cout << "Closest Standard Value = 0.62" << endl;
cout << "Color Band A : blue" << endl;
cout << "Color Band B : gray" << endl;
}
else    if(  result > 68 && result <= 75  )
{
cout << "Closest Standard Value = 0.68" << endl;
cout << "Color Band A : violet" << endl;
cout << "Color Band B : green" << endl;
}
else    if(  result > 75 && result <= 82  )
{
cout << "Closest Standard Value = 0.75" << endl;
cout << "Color Band A : gray" << endl;
cout << "Color Band B : red" << endl;
}
else    if(  result > 82 && result <= 91  )
{
cout << "Closest Standard Value = 0.82" << endl;
cout << "Color Band A : white" << endl;
cout << "Color Band B : brown" << endl;
}
else     if(  result > 91 )
{
cout << "Closest Standard Value = 0.91" << endl;
cout << "Color Band A : white" << endl;
cout << "Color Band B : brown" << endl;
}

else
{
cout << "Invalid Input! Would you like to try again? Enter Y: ";
}
}
}

do
{
if(tol == 10)
{

if( result <= 10 )
{
cout << "Closest Standard Value = 0.10" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : black" << endl;
}

else if( result > 10 && result <=12 )
{
cout << "Closest Standard Value = 0.12" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : red" << endl;
}
else  if( result > 12 && result <=15 )
{
cout << "Closest Standard Value = 0.15" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : green" << endl;
}
else  if(  result > 15 && result <=18 )
{
cout << "Closest Standard Value = 0.18" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : gray" << endl;
}
else  if(  result > 18 && result <=22  )
{
cout << "Closest Standard Value = 0.22" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : red" << endl;
}
else  if(  result > 22 && result <= 27  )
{
cout << "Closest Standard Value = 0.27" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : violet" << endl;
}
else  if(  result > 27 && result <=33  )
{
cout << "Closest Standard Value = 0.33" << endl;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : orange" << endl;
}
else  if(  result > 33 && result <= 39  )
{
cout << "Closest Standard Value = 0.39" << endl;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : white" << endl;
}
else  if(  result > 39 && result <= 47  )
{
cout << "Closest Standard Value = 0.47" << endl;
cout << "Color Band A : yellow" << endl;
cout << "Color Band B : violet" << endl;
}
else  if( result > 47 && result <=56 )
{
cout << "Closest Standard Value = 0.56" << endl;
cout << "Color Band A : green" << endl;
cout << "Color Band B : blue" << endl;
}
else  if(  result > 56 && result <= 68  )
{
cout << "Closest Standard Value = 0.68" << endl;
cout << "Color Band A : blue" << endl;
cout << "Color Band B : gray" << endl;
}
else  if(  result > 68 && result <= 82  )
{
cout << "Closest Standard Value = 0.82" << endl;
cout << "Color Band A : gray" << endl;
cout << "Color Band B : red" << endl;
}
else
{
cout << "Invalid Input! Would you like to try again? Enter Y: ";
}
}
}

do
{
if(tol == 20)
{

if( result <= 10 )
{
cout << "Closest Standard Value = 0.10" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : black" << endl;
}

else if( result > 10 && result <=15 )
{
cout << "Closest Standard Value = 0.15" << endl;
cout << "Color Band A : brown" << endl;
cout << "Color Band B : green" << endl;
}
else  if( result > 15 && result <=22 )
{
cout << "Closest Standard Value = 0.22" << endl;
cout << "Color Band A : red" << endl;
cout << "Color Band B : red" << endl;
}
else  if(  result > 22 && result <=33 )
{
cout << "Closest Standard Value = 0.33"  << endl;
cout << "Color Band A : orange" << endl;
cout << "Color Band B : orange" << endl;
}
else  if(  result > 33 && result <= 47  )
{
cout << "Closest Standard Value = 0.47" << endl;
cout << "Color Band A : yellow" << endl;
cout << "Color Band B : violet" << endl;
}
else  if(  result > 47 && result <= 68  )
{
cout << "Closest Standard Value = 0.68" << endl;
cout << "Color Band A : blue" << endl;
cout << "Color Band B : gray" << endl;
}
else
{
cout << "Invalid Input! Would you like to try again? Enter Y: ";
}
}
}

}
system("pause");
return 0;
}////////////////////////Function Definition//////////////////////////////////
string NumberConv (char number)
{
if (number == '1')
return "brown";
else if (number == '2')
return "red";
else if (number == '3')
return "orange";
else if (number == '4')
return "yellow";
else if (number == '5')
return "green";
else if (number == '6')
return "blue";
else if (number == '7')
return "violet";
else if (number == '8')
return "gray";
else if (number == '9')
return "white";
}
//----------------------------For Bands-----------------
int Converter (string Color)
{
if (Color == "black")
return 0;
if (Color == "brown")
return 1;
if (Color == "red")
return 2;
if (Color == "orange")
return 3;
if (Color == "yellow")
return 4;
if (Color == "green")
return 5;
if (Color == "blue")
return 6;
if (Color == "violet")
return 7;
if (Color == "gray")
return 8;
if (Color == "white")
return 9;
if (Color == "silver")
return -1;
if (Color == "gold")
return -2;
else
return 10;
}
//----------------------------For Band D-----------------
int ConverterD (string D)
{
if (D == "gold")
return 5;

if (D == "silver")
return 10;

if (D == "none")
return 20;

else
return 0;
}
//----------------------------For Band D-----------------
string ConverterD (int D)
{
if (D == 5)
return "gold";

if (D == 10)
return "silver";

if (D == 20)
return "none";

else
return 0;
}
//----------------------------For Band A-----------------
int ConverterA( string Color)
{
if (Color == "brown")
return 1;
if (Color == "red")
return 2;
if (Color == "orange")
return 3;
if (Color == "yellow")
return 4;
if (Color == "green")
return 5;
if (Color == "blue")
return 6;
if (Color == "violet")
return 7;
if (Color == "gray")
return 8;
if (Color == "white")
return 9;
else
return 10;
}
//----------------------------For Band B-----------------
int ConverterB( string Color)
{
if (Color == "black")
return 0;
if (Color == "brown")
return 1;
if (Color == "red")
return 2;
if (Color == "orange")
return 3;
if (Color == "yellow")
return 4;
if (Color == "green")
return 5;
if (Color == "blue")
return 6;
if (Color == "violet")
return 7;
if (Color == "gray")
return 8;
if (Color == "white")
return 9;
else
return 10;
}``````

## jonsca 1,059

I ran it through a couple of times:

``````Is the ohm a decimal? Enter Y : y

Please enter 0 first then the decimal point. (Ex. 0.1)
value in ohms (0.01 ohms to 100,000,000 ohms): 1.0

Please enter the desired tolerance as a percentage(5, 10, or 20): 10

Results:
Closest Standard Value = 0.10
Color Band A : brown
Color Band B : black

-------------------------------------------------------------

Is the ohm a decimal? Enter Y : y

Please enter 0 first then the decimal point. (Ex. 0.1)
value in ohms (0.01 ohms to 100,000,000 ohms): 2.25

Please enter the desired tolerance as a percentage(5, 10, or 20): 20

Results:
Closest Standard Value = 0.33
Color Band A : orange
Color Band B : orange``````

The only thing I changed was your `itoa` s into `sprintf` s for compatibility purposes.

There may be other issues going on with it, but it doesn't quit after the tolerances on my machine.

## sfuo 111

There are quite a few problems with this code.

#1) The run-time error is caused because you are trying to remove the first two digits of your decimal input and it is crashing because you use the itoa() function which takes an integer and converts it into a cstring. You are using a double which if it is less than 0 it will be put in as 0 (because an integer is a whole number and does not hold any decimal places). Then you try to substring that and say start at 2 and copy the next 2 elements. Since your string is only holding "0" you are trying to cut it out of bounds therefore making it crash.

#2) Another problem that you would run into later would be the fact that you are allowing an input of "0.01". This resistor would be Brown, Black, "??" (something that is 10^-3), Tolerance which doesn't exist on the 4 band resistors. Meaning you have to limit it to "0.1" because that would be Brown, Black, Silver, Tolerance.

To solve problem #1 I would completely scrap the function itoa() since it is old and uses cstrings when you are just converting them into strings afterward anyways. Use the stringstream object (you already have the header added and use it elsewhere) to insert your resistance after doing bounds checking then find the two significant digits and where the decimal place is to determine the multiplier.

I have attached a version of this that I wrote using some of your code. I'm posting it up so you can learn from it since it is a bit less "hard coded" and cuts down on length.

commented: Nice job +14

## mike_2000_17 2,669

After inputting a single character or a number, you should always make a call to `cin.ignore();` .

For the error, I can't be sure.

Try to avoid using C-style strings, they can yield surprising errors. I suggest you use a stringstream based conversion from both appearances of itoa(). As follows, for example:

``string AnB = (stringstream() << hex << resist).str(); //instead of itoa(resist,AnB,16);``

Then, I say this line 133:

``anb = countAnB.substr(2,2);``

How do you guarantee that the string countAnB will actually contain a sub-string of length 2 starting from position 2. If countAnB is not long enough, this will crash.

Line 137 is also suspect. You are storing the number as a base-16 (hexadecimal) string, then converting to "result", but your conversion assumes decimal (base-10) digits. This could also crash very easily. You should use the same method for input as for output, thus:

``if(!(convert >> hex >> result))``