I'm writing a program to convert Decimal numbers to binary, hexadecimal, and BCD as a refresher before i start getting code assignments in school. I'm almost done, but i have a seg_fault (compiler says decToHex function) in 1 function and i am hoping for some feedback on the others.
I'll break it up a little so it's not 1 giant block of code and attach the .CPP
Class Definition
class ConvertDecimal {
public:
ConvertDecimal();
//void print();
private:
int MAX_SIZE;
void Convert ();
string decToBinary (int decimal);
string decToHex (int decimal);
string decToBCD (int decimal);
char hex(int num);
string BCD(char num);
vector <int> decNumbers;
vector<string> HexNumbers;
vector<string> BCDNumbers;
vector<string> BinaryNumbers;
};
Initialize and Convert functions
ConvertDecimal::ConvertDecimal() { //Initialize decNumbers array
MAX_SIZE = 256;
decNumbers.resize(MAX_SIZE);
HexNumbers.resize(MAX_SIZE);
BCDNumbers.resize(MAX_SIZE);
BinaryNumbers.resize(MAX_SIZE);
for (int i = 0; i < MAX_SIZE; i++) {
decNumbers[i] = i;
//cout << decNumbers[i] << " ";
}
Convert();
}
void ConvertDecimal::Convert () {
for (int i = 0; i < MAX_SIZE; i++) {
BinaryNumbers[i] = decToBinary(i);
//decToBCD(i);
decToHex(i);
}
}
decToBinary
int quotient = 1;
int temp = decimal;
string remainder;
string binaryNum = "2";
while (quotient!=0) {
remainder = temp%2;
quotient = temp/2;
temp = quotient;
binaryNum = binaryNum + remainder;
}
binaryNum.erase(0);
cout << binaryNum;
return binaryNum;
}
hex and decToHex
char ConvertDecimal::hex(int num) {
if (num==10) { return 'A'; }
else if (num==11) { return 'B';}
else if (num==12) { return 'C';}
else if (num==13) { return 'D';}
else if (num==14) { return 'E';}
else return 'F';
}
string ConvertDecimal::decToHex(int decimal) {
string hex;
int num,y,i;
int n=1;
int * r;
char h[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
y = num = decimal;
while(num >= 10)
{
num/=10;
++n;
}
r=new int[n];
for(i=0 ; y >= 16 ; ++i)
{
r[i]=y%16;
y /= 16;
}
r[i++]=y;
for(i=(n-1) ; i>=0 ; --i)
{
hex+=h[r[i]]; //Compiler says seg_fault is here
cout<<h[r[i]];
}
return hex;
}
string ConvertDecimal::decToBCD(int decimal) {
int i = decimal;
string s, temp, final;
stringstream out;
out << i;
s = out.str();
for (int i = 0; i < s.size(); i++){
temp = BCD(s[i]);
final = final+temp;
}
return final;
}
BCD and decToBCD
string ConvertDecimal::decToBCD(int decimal) {
int i = decimal;
string s, temp, final;
stringstream out;
out << i;
s = out.str();
for (int i = 0; i < s.size(); i++){
temp = BCD(s[i]);
final = final+temp;
}
return final;
}
string ConvertDecimal::BCD(char num) {
switch (num) {
case '0':
return "0000";
break;
case '1':
return "0001";
break;
case '2':
return "0010";
break;
case '3':
return "0011";
break;
case '4':
return "0100";
break;
case '5':
return "0101";
break;
case '6':
return "0110";
break;
case '7':
return "0111";
break;
case '8':
return "1000";
break;
case '9':
return "1001";
break;
default:
return "2"; /*Just a random number not part of the binary system, if there's a better way to 'flag' an error let me know*/
break;
}
}
Main
int main () {
ConvertDecimal zoidberg;
//zoidberg.print();
return 0;
}
Really just looking for feedback on algorithms and a seg_fault fix. I'll add the print function when i get to it.