Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm attempting to condense the following C++ code down where there's only one input for binary to decimal conversion. Right now the current code is

I'm attempting to condense the following C++ code down where there's only one input for binary to decimal conversion. Right now the current code is prompting for binary to decimal twice, and it should only be prompting for binary to decimal once, while ensuring that the user is only entering 1's and 0's. If they enter a 2, 3, etc it should prompt the user to enter an acceptable binary number again.

Note: The decimal to binary conversion is correct, so please don't alter that one.

#include #include #include using namespace std;

string bn;

int input(); // function to Input of Base 2 value to enforce the entry of only 1's and 0's

int Base10toBase2(long long); // Base 10 to Base 2

int Base2toBase10(long long); //Base 2 to Base 10

int main(){ long long n,o; cout > n; input(); cout > o; cout

int Base10toBase2(long long n){ long long BinaryNo = 0; int remainder, i = 1, step = 1; while (n!=0){ remainder = n%2; n /= 2; BinaryNo += remainder*i; i *= 10; } return BinaryNo; }

int Base2toBase10(long long o){ int DecimalNo = 0, i = 0, remainder; while (o!=0){ remainder = o%10; o /= 10; DecimalNo += remainder*pow(2,i); ++i; }

return DecimalNo; }

int input(){ int x, count, repeat = 0; while (repeat == 0){ cout "; cin >> bn; count = bn.length(); repeat = 1; for (x = 0; x

Here's a current example of the output that's going on. That second and third line of input should be condensed into one user input, where it ensures the user is only entering 1's and 0's, and re-prompting for an input if anything other than 1's and 0's are entered.

image text in transcribed

Enter a decimal number: 29 Enter a binary number only in only 1's and O's => 100010 Enter a binary number: 1001 29 in decimal = 11101 in binary 1001 in binary = 9 in decimal - - - - - - - - - - - - - - Process exited after 11.4 seconds with return value o Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

what about Charitable Contributions?

Answered: 1 week ago