Originally Posted by
whiteflags
It works for any binary number up to 4 digits you can think of, as well as others, if you bothered to compute the other place values. I'd prove it to you, but at this point you know everything you need to, so do it and see for yourself.
I think I'm getting myself more confused. How do I take those numbers and actually make it work?
I've been on this all night and haven't gotten anywhere.
Here is the code I have and the problem with it.
Code:
#include<iostream>
using namespace std;
int main()
{
int binary1, binary2, binary3, binary4, decimal1, decimal2, decimal3, decimal4, total;
cout << "Enter a 4-digit binary number: " ;
cin >> binary1 >> binary2 >> binary3 >> binary4;
decimal1 = binary4 * 1;
decimal2 = binary3 * 2;
decimal3 = binary2 * 4;
decimal4 = binary1 * 8;
total = decimal1 + decimal2 + decimal3 + decimal4;
cout << "The decimal equivalent of " << binary4 << binary3 << binary2 << binary1 << " is " << total << endl;
return 0;
}
So in this code I wrote it will convert any binary number to decimal, however the problem is the user has to put the binary number in 1 number at a time, with either a space or enter.
I don't know how to do the calculation with just one statement. So the user could just enter the number as one number, instead of 4.