Using C++, Allow a user to input a series of zeros' and one's. stored in an array representing a binary word.

Then display the word horizontally on one line of the screen.

Then the program finds the decimal equilivant on the screen.

I would like some pointers on solving this problem.
I've set the array to hold a binary word. There is a display to enter on zero's and ones up to eight. I am not sure converting binary to decimal, problem converting it to decimal.

Thank you.

MichaelB

Once you have the bit string it's pretty easy. You'll just need to loop from the least significant bit (the one whose value is 1, often the right-most one) through the string to the most significant bit (the one whose value is 2^n, corresponding usually to the left-most). If the bit at the current index is 1, add 2^i, where i is the current index. For finding powers of two, you could also use the left-shift operator (number << shiftAmt), which would pretty much be 1<<i for this case. Make sense?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.