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.

## All 7 Replies

It appears as if you're not properly splitting up your input when the input is greater than 9 at the following code:

``````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]];
cout<<h[r[i]];
}``````

I believe that h[r] is not properly calculated once you hit 10 and greater as a input.

But anyway, it's a suggested starting point.

Actually, I think the problem is that n is too large (since you determine the number of base-10 digits you need to represent the integer, by repeatedly dividing by 10, rather than the smaller number of hex digits you need). You then fill most of the array r, correctly dividing by 16 each time. But you don't keep save the final number of values actually stored (the value of i, following the last assignment into r at line 28 of the correct block of code above). Then you reverse your values (correct idea!) but start at the end of the array. If you didn't store a value into position n-1, then it probably has a garbage value in there, and your seg_fault is because you try to append the hex-digit from the 42954383247'th (or whatever) element of your h array, but you have only 16 entries!

Gonna try that once I get home, browsing from my phone at work right now. Thanks for the tips!

Change this code:

``````while(num >= 10)
{
num/=10;
++n;
}``````

to this code:

``````while(num >= 16)
{
num/=16;
++n;
}``````

BobS037,

Exactly. But on DaniWeb, we strive to have people fix up their own code, rather than just doing it for them. It might take a bit longer, but people learn more, and learn better, by "doing it themselves" with some help. Cheers!

Here's everything, it works now but something is wrong with the output. Binary is not workin. Gonna mark as solved (if i can figure out how)

``````#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

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

ConvertDecimal::ConvertDecimal() {
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);
BCDNumbers[i] = decToBCD(i);
HexNumbers[i] = decToHex(i);
}
}
string ConvertDecimal::decToBinary(int decimal) {
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;
}
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 >= 16)
{
num/=16;
++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]];
//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;
}

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

void ConvertDecimal::print() {
ofstream outFile ( "MattGildner_Program0_CIS310" );

outFile << " Decimal	Hexadecimal		      	Binary			   		BCD			" << endl;
for (int i = 0; i<MAX_SIZE; i++) {
outFile << decNumbers[i] << "       " << BinaryNumbers[i];
outFile << "          " << HexNumbers[i] << "         " << BCDNumbers[i] << endl;
}
}

int main () {

ConvertDecimal zoidberg;
zoidberg.print();

return 0;
}``````

As for binary, looks like you're building the string in reversed-order? In decToHex, you store your remainders in reversed-order, and then iterate through backwards to build the string in forward order. Try doing exactly the same thing for decToBinary (except, of course, in base-2).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.