Question: I am using C++ to write a program that converts from Binary to Decimal and vice versa using recursion. Recursion means a function calls itself.
I am using C++ to write a program that converts from Binary to Decimal and vice versa using recursion. Recursion means a function calls itself. The sample output I am looking for is this...
Decimal-Binary Conversions By xxxxxxxxxxxxxxxxxxxxx
Dec2Bin or Bin2Dec or Quit? (D/B/Q): D
Please enter your decimal value to be converted (0 to quit): 320
Decimal 320 divided by 2 equals 160 with a remainder of: 0
Decimal 160 divided by 2 equals 80 with a remainder of: 0
Decimal 80 divided by 2 equals 40 with a remainder of: 0
Decimal 40 divided by 2 equals 20 with a remainder of: 0
Decimal 20 divided by 2 equals 10 with a remainder of: 0
Decimal 10 divided by 2 equals 5 with a remainder of: 0
Decimal 5 divided by 2 equals 2 with a remainder of: 1
Decimal 2 divided by 2 equals 1 with a remainder of: 0
Decimal 1 divided by 2 equals 0 with a remainder of: 1
Therefore, by recursion, the binary value of 320 is: 101000000
Please enter your decimal value to be converted (0 to quit): 0
Dec2Bin or Bin2Dec or Quit? (D/B/Q): B
Please enter your binary value to be converted (0 to quit): 1010
There is a 2 in the value (2^1)
There is a 8 in the value (2^3)
Therefore the decimal value is: 10
Please enter your binary value to be converted (0 to quit): 0
Dec2Bin or Bin2Dec or Quit? (D/B/Q): Q
Thanks for using the Decimal-Binary converter!
The code I have so far is as follows
#include "stdafx.h" #include#include #include using namespace System; using namespace std; void ConvertByRecursion(long long n); long long getDecimal(); long long getBinary(); long long BinaryToDecimal(long long); int main() { long long n, result; char choice; cout << "Decimal-Binary Conversions By Fedaa Musleh" << endl; cout << "Welcome to the Decimal to Binary Converter!" << endl; while (choice != 'Q' || 'q') { cout << "Dec2Bin or Bin2Dec or Quit? (D/B/Q): "; cin >> choice; if (choice == 'D' || 'd') { n = getDecimal(); while (n !=0) { ConvertByRecursion(n); cout << endl << endl; n = getDecimal(); } } else if (choice == 'B' || 'b') { n = getBinary(); result = BinaryToDecimal(n); cout << " Therefore the binary value is: " << result << endl; cout << " Thanks for using the decimal converter!" << endl; } } cout << " Thanks for using the Decimal-Binary converter!" << endl; system("Pause"); return 0; } long long getDecimal() { long long n; bool baddata = false; do { baddata = false; cout << "Please enter your decimal value to convert (0 to quit): "; cin >> n; if(!cin.good()) { cin.clear(); cin.ignore(numeric_limits ::max(), ' '); 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; } void ConvertByRecursion(long long n) { long long r; long long newval; r = n % 2; newval = n / 2; Console::WriteLine("Decimal {0,12:D} divided by 2 = {1, 12:D} w/Remainder of: {2,3:D} ", n, newval, r); if (newval > 0) { ConvertByRecursion(newval); } else { cout << " Therefore, by recursion, the binary value of " << n << " is: "; } cout << r; } 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(), ' '); 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; }
my BinaryToDecimal() function should be rewritten so that it uses the recursion method and outputs the same message found in the sample output above. Also, the user input (D/B/Q) is not working properly in main() and always reads input as 'D' instead of 'B' or 'Q'. Please fix these two mistakes and be sure the sample output for the BinaryToDecimal() function is formatted the same as in the sample ouput above.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
