Question
How can I alter my C++ code below to eliminate the infinite loop that's currently in my main driver? I'm trying to simplify the main
How can I alter my C++ code below to eliminate the infinite loop that's currently in my main driver? I'm trying to simplify the main driver down. I'm prompting the user to enter a decimal number and a binary number, and convert the decimal number to a binary number, and the binary number to a decimal number.
#include #include #include using namespace std;
int BinaryEnforcer(long long); int PositiveBase10(long long); string Base10toBase2(long long); long long Base2toBase10(long long);
int main() { long long dec, bin; while (1) { cout << "Enter a positive integer in Base 10: "; long long 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: binaryString //*********************************************** string Base10toBase2(long long dec) { long long remainder, i = 1; string binaryString; while (dec != 0) { remainder = dec % 2; if(remainder==1) { binaryString = "1" + binaryString; } else { binaryString = "0" + binaryString; } dec /= 2; i *= 10; }
return binaryString; }
//*********************************************** // Function Name: Base2toBase10 // Incoming Parameters: 'bin' , long type number (nothing but taking the binary number) // Outgoing Parameters: N/A // Return Value: decimalNum //*********************************************** long long Base2toBase10(long long bin) { long long 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: 'num' , long type number (nothing but taking the binary number) // Outgoing Parameters: N/A // Return Value: 0 or 1 //*********************************************** int BinaryEnforcer(long long num) { long long x; while (num) { x = num % 10; if ((x == 0) || (x == 1)) { num = num / 10; }
else { return 0; } } return 1; }
//*********************************************** // Function name: PositiveBase10 - ensures the Base 10 number entered by the user is a positive integer // Incoming Parameters: 'x' , long type number (nothing but taking the decimal number) // Outgoing Parameters: N/A // Return Value: 0 or 1 //*********************************************** int PositiveBase10(long long x){ if(x < 0) return 0; if(100 * x != (100 * 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