Help Please! Binary 2 Decimal conversion using recursion.

I need to write a code that validates a user's entry as only 1's and 0's, and converts it into a decimal value. I am required to use recursion to do this. I am new to recursion and am still confused about it. This is what I have so far. It is also important that I have validation so that if 123 is entered, an error message is given with the option to re enter the binary value to be converted. If anyone can help me out I will be greatly appreciative.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
long long getBinary()
{
	long long n;
	bool baddata = false;
	do
	{
		baddata = false;
		cout << "Please enter your Binary value to convert to Decimal: (0 to Quit). ";
		cin >> n;
		if (!cin.good())
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "I could not decipher your input as a decimal value " << endl;
			baddata = true;
		}
		else if (n < 0)
		{
			cout << "Please enter a non-negative value." << endl;
			baddata = true;
		}
	} while (baddata);
	
	return n;
}

long long BinaryToDecimal(long long n)
{
	long long value = 0, i = 0, binarydigit, position;
	while (n != 0)
	{
		binarydigit = n % 10;
		position = pow(2,i);
		value = value + binarydigit*position;
		n = n/10;
		i++;
	}

	return value;
}
Last edited on
Transforming the while loop you made into a recursive function shouldn't be too difficult. Do something like this:
1
2
3
4
5
int BinaryToDecimal(int n, int pos)
{
   if (pos == to_string(n).length()) return n; 
   return ((int)to_string(n)[to_string(n).length()-pos-1]-48)*pow(2,pos+1) + BinaryToDecimal(n,pos+1);
}

I have no idea if this works because I wrote it really quickly but i hope the idea can help you!
@imprudentspace

Here's one way to do it and still use recursion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

void Get_binary(std::string &binary);
void Converted_binary(std::string binary, int len, int &decimal, int number);

int main()
{
	std::string binary = "";
	int decimal = 0, number = 1;
	std::cout << "Please enter your binary number.." <<std::endl;
	Get_binary(binary);
	int len = binary.length()-1; // To start from the far right location of string
	Converted_binary(binary, len, decimal, number);
	std::cout << std::endl << "The entered binary number equals " << decimal << std::endl;
	return 0;
}

void Get_binary(std::string &binary)
{
	char zero_1;
	bool ok;
	std::cout << "Enter 'x' to end binary inputs" << std::endl;
	do
	{
		ok = true;
		binary = "";
		do
		{
			std::cin >> zero_1;
			if(zero_1 == '0' || zero_1 == '1')
				binary+=zero_1;
			else if (zero_1 != 'x' )// Edited.
                      // Did not need '&& zero_1 !='0' && zero_1 !='1';
			{
				std::cout << std::endl << "Please enter only binary numbers consisting of '0's and '1's" << std::endl << std::endl;
				ok = false; // If not a 0, 1 or an x (to end binary input)
			}
		}while(zero_1 != 'x');
	}while(!ok);
}

void Converted_binary(std::string binary, int len, int &decimal, int number)
{
	if(binary[len] == '1')
		decimal+=number;
	number*=2;
	len--;
	if(len != -1)
		Converted_binary(binary, len, decimal, number);
}
Last edited on
That's somewhat the same as I wrote (i think), but much much clearer :)
Use a string to represent the binary - the numbers of 0's and 1's gets very large.

Use string member function find_first_not_of( "01" ) for the validation. One line.

To convert binary string abcd, recursion looks like:
decimal rep of abcd = 2 * (decimal rep of abc) + digit for d
Roughly 2-3 lines.
Last edited on
Topic archived. No new replies allowed.