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()
{ 
     char answer;

do{
     answer = 'n';
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: " ;
cin >> answer;
}
}
while (answer == 'y' || answer == 'Y');

return Resistances;
}

double Resistance:: getTolerance()
{
    char answer;
do{
      answer = 'n';
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: " ;
cin >> answer;
}
}
while (answer == 'y' || answer == 'Y');


return Tolerance;
}
    
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;
    char answer, deci, AnB[15], C[10];
    
    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
 {

  answer = 'n';
 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: ";
  cin >> answer;
  }
}  
}

while (answer =='Y' || answer=='y');

 do
 {
 answer = 'n';
 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: ";
  cin >> answer;
  }
}  
}

while (answer =='Y' || answer=='y');

do
 {
 answer = 'n';
 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: ";
  cin >> answer;
  }
}  
}

while (answer =='Y' || answer=='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;
}

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)
Please enter the desired resistance
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)
Please enter the desired resistance
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.

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.

Edited 5 Years Ago by sfuo: forgot to add file

Comments
Nice job
Attachments
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

class RESISTOR
{
	double resistance;
	int tolerance;
	static vector<string> bands; //static so you can assign it without copying/assigning in a constructor
	static string colors[]; //same as above

	public:

	bool SetResistance(); //gets user input and sets the first 3 bands
	bool SetTolerance(); //gets user input and sets the 4th band
	void DisplayBands(); //displays the 4 bands
};

vector<string>RESISTOR::bands(4, "Black"); //creates 4 "bands" that are default Black (incase user types in "1" for resistance then 2nd band will be black by default)
//instead of constantly typing out all the colors create an array and use basic math to set colors of the bands
string RESISTOR::colors[] = {"Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Gray", "White", "Gold", "Silver", "None"};

int main()
{
	RESISTOR ohm; //call it whatever
	if(ohm.SetResistance()) //if goes wrong in setting the value it just quits
		if(ohm.SetTolerance()) //if goes wrong in setting the value it just quits
			ohm.DisplayBands(); //display the 4 bands

	return 0;
}

bool RESISTOR::SetResistance()
{
	double resistNum;
	string resistStr;
	int multiplier, decimalLoc, secondSigDigitLoc;
	char answer;

	do
	{
		cout << "\nPlease enter the desired resistance \nvalue in ohms (0.1 ohms to 100,000,000 ohms): ";
		cin >> resistNum;
		answer = 'n';
		if( resistNum < 0.1 || resistNum >= 100000000 ) //use OR not AND otherwise it will never be true
		{
			cout << "Invalid Input! Would you like to try again? Enter Y: " ;
			cin >> answer;
			if( answer != 'Y' && answer != 'y' )
				return false;
		}
	} while(answer == 'y' || answer == 'Y');

	stringstream strm;
	strm << fixed << resistNum; //use fixed otherwise it will convert large numbers to scientific notation
	strm >> resistStr;
	cout << resistStr << endl;
	//finds the first two significant numbers (band 1 and 2)
	for( unsigned int i = 0, c = 0; i < resistStr.length() && c < 2; i++ )
		if( !(c == 0 && resistStr[i] == '0') && resistStr[i] != '.' )
		{
			bands[c] = colors[resistStr[i]-'0']; //converts digit (that is a char) to an int
			c++;
		}

	//finds the position of the decimal
	secondSigDigitLoc = -1;
	for( unsigned int i = 0; i < resistStr.length(); i++ )
	{
		if( resistStr[i] == '.' ) //there will always be a decimal because the number was inserted with "fixed"
			decimalLoc = i-1;
		else if( resistStr[i] != '0' && secondSigDigitLoc == -1 )
			secondSigDigitLoc = i+1;
		if( i == resistStr.length()-1 && secondSigDigitLoc == -1 )
			secondSigDigitLoc = 0;
	}
	//calculate and assign a color to the 3rd band
	multiplier = decimalLoc-secondSigDigitLoc;
	if( multiplier < 0 )
		bands[2] = colors[(multiplier*-1)+9]; //if it is negative then multiply the number by -1 and add 9 to it to get the color index
	else
		bands[2] = colors[multiplier];
	return true;
}

bool RESISTOR::SetTolerance()
{
	int tolerance;
	char answer;

	do
	{
		cout << "\nPlease enter the desired tolerance as a percentage(5, 10, or 20): ";
		cin >> tolerance;
		answer = 'n';
		if (tolerance != 5 && tolerance != 10 && tolerance != 20)
		{
			cout << "Invalid Input! Would you like to try again? Enter Y: " ;
			cin >> answer;
			if( answer != 'Y' && answer != 'y' )
				return false;
		}
	} while(answer == 'y' || answer == 'Y');

	bands[3] = colors[(tolerance/10)+10]; //this only works for 5, 10, 20 because 5/10 = 0, 10/10 = 1, 20/10 = 2, add 10 and that gives you the index of the color
	return true;
}

void RESISTOR::DisplayBands()
{
	for( unsigned int i = 0; i < bands.size(); i++ )
		cout << i+1 << ": " << bands[i] << endl;
}

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))
This article has been dead for over six months. Start a new discussion instead.