Can u help me guys. I have problem of calling function from class and overloading. When I'm trying to print short int type number, the program brakes down. No errors are shown which tell me whats wrong.
#include <cstdlib>
#include <iostream>
using namespace std;
int i=0, j=0, zero_fill, l=0;
class dec_to_bin
{
public:
char bin[33], rev[33];
char *to_bin(unsigned int dec, int size);
char *to_bin(unsigned short int dec, int size);
};
char *dec_to_bin :: to_bin(unsigned int dec, int size)
{
while(dec > 0) {
bin[i] = dec%2+48;
dec = dec/2;
i++;
}
zero_fill = size-i;
while(l<zero_fill) {
bin[i+l] = '0';
l++;
}
while(j < i+l) {
rev[j] = bin[i+l-j-1];
j++;
}
rev[j] = 0;
return rev;
}
char *dec_to_bin :: to_bin(unsigned short int dec, int size)
{
to_bin(dec,size); //using same function again
}
int main(int argc, char *argv[])
{
unsigned int num = 20;
unsigned short int num1 = 21;
dec_to_bin b;
printf("bin int: %s\n", b.to_bin(num, 32)); //works fine
printf("bin short: %s\n", b.to_bin(num1, 16)); //program breakes down
system("PAUSE");
return EXIT_SUCCESS;
}