### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````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::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:
 ``12345`` ``````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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include 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.." <> 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.