
-----------------------------------
gitoxa
Tue Dec 09, 2008 6:16 pm

Decimal to Binary
-----------------------------------
I'm fairly new to c++ and I've been having trouble trying to code a recursive decimal to binary converter.  I'm trying to pass an integer into the function and return a string in the end.  I'm not sure if I'm going about this the right way though.  Tips preferred over solutions if possible, as I'm trying to learn. :)

Here's my code which compiles fine in Dev-CC++ 4.9.9.2, but I get some... weird output.

#include 
#include 
using std::string;

//A function that convers a decimal number to a binary number, stored in a string variable
string Dec_To_Bin(int Num)
{
	if (Num > 0)
	{
		return Dec_To_Bin(Num / 2) + (char) (Num % 2);
	}
	else
	{
		return "";
	}
}

int main()
{
	int DecNum;
	string BinNum;
	
	std::cout > DecNum;
	
	BinNum = Dec_To_Bin(DecNum);
	std::cout 