Question
I cannot get my C++ code to implement the Base 10 to Base 2 conversion correctly. It keeps outputting numbers other than 1's and 0's.
I cannot get my C++ code to implement the Base 10 to Base 2 conversion correctly. It keeps outputting numbers other than 1's and 0's. Can someone help?
#include
int binaryEnforcer(long long); int positiveBase10(double); int Base10toBase2(long long); int Base2toBase10(long long);
int main() { long long dec, bin; while (1) { cout << "Enter a positive integer in Base 10: "; double x; cin >> x; if (positiveBase10(x)) { dec = x; break; } else { cout << "Number entered is not a positive integer. Please try again." << endl; } }
while (1) { cout << "Enter a binary number: "; cin >> bin; if (binaryEnforcer(bin)) { break; }
else { cout << "Number entered is not a binary number. Please try again." << endl; } }
cout << endl; cout << dec << " in decimal = " << Base10toBase2(dec) << " in binary" << endl; cout << bin << " in binary = " << Base2toBase10(bin) << " in decimal"; return 0;
}
//*********************************************** // Function Name: Base10toBase2 // Incoming Parameters: 'dec' , long type number (nothing but taking the decimal number) // Outgoing Parameters: N/A // Return Value: binaryNum //*********************************************** int Base10toBase2(long long dec) { long long binaryNum = 0; int remainder, i = 1; while (dec != 0) { remainder = dec % 2; dec /= 2; binaryNum += remainder * i; i *= 10; }
return binaryNum; }
//*********************************************** // Function Name: Base2toBase10 // Incoming Parameters: 'bin' , long type number (nothing but taking the binary number) // Outgoing Parameters: N/A // Return Value: decimalNum //*********************************************** int Base2toBase10(long long bin) { int decimalNum = 0, i = 0, remainder; while (bin != 0) { remainder = bin % 10; bin /= 10; decimalNum += remainder * pow(2, i); ++i; }
return decimalNum; }
//*********************************************** // Function Name: binaryEnforcer - input of Base 2 value to enforce the entry of only 1s and 0s // Incoming Parameters: 'bin' , long type number (nothing but taking the binary number) // Outgoing Parameters: N/A // Return Value: 0 or 1 //*********************************************** int binaryEnforcer(long long bin) { long long x; while (bin) { x = bin % 10; if ((x == 0) || (x == 1)) { bin = bin / 10; }
else { return 0; } } return 1; }
//*********************************************** // Function name: positiveBase10 - ensures the Base 10 number entered by the user is a positive integer // Incoming Parameters: 'dec' ,double type number (nothing but taking the decimal number) // Outgoing Parameters: N/A // Return Value: 0 or 1 //*********************************************** int positiveBase10(double x){ if(x<0) return 0; if(1000*x!=(1000*int(x))) return 0; return 1; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started