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;
}