// Convert decimal to binary 2
// By DreamVB 20:42 15/10/2016

#include <iostream>

using namespace std;
using std::cout;
using std::endl;

char *Int2Bin(int number){
	char bin[31] = { '0' };
	char *sBin = NULL;
	int value = number;
	int i = 0;
	int r = 0;

	//Convert int to binary string
	while (value != 0){
		//Get remainder
		r = value % 2;
		//Keep divideing
		value /= 2;
		//Fill the binary char array with 1 or 0
		bin[i] = r + '0';
		//INC counter
		i++;
	}
	//If i is zero set to 1 for null char placement
	if (i == 0){
		i = 1;
	}
	//Set nullchar
	bin[i] = '\0';
	//Copy char array to pchar
	sBin = (char*)malloc(i);
	strcpy(sBin, strrev(bin));

	//Clear up old bin array
	memset(bin, 0, sizeof(bin));
	//Turn string around and return.
	return sBin;
}

int main(int argc, char *argv[]){
	int dnum = 0;
	char ch = '\0';

	while (1){
		system("cls");
		cout << "Enter a decimal number to convert to binary : ";
		//Get number.
		cin >> dnum;
		cout << endl;
		//Write out binary value
		cout << dnum << " in binary is : " << Int2Bin(dnum) << endl << endl;
		
		//Ask user do they want to convert agian.
		cout << "Do you want to convert another number (Y/N) : ";
		cin >> ch;
		
		if ((ch == 'n') || (ch == 'N')){
			cout << endl << "Thanks for trying the program." << endl;
			break;
		}
		else{
			if ((ch != 'y') && (ch !='Y')){
				cout << "Syntex error program will now exit." << endl;
				break;
			}
		}
	}
	
	//Pause
	system("pause");
	return 0;
}