Convert a binary number into a decimal number

• 07-05-2013
HTHVampire
Convert a binary number into a decimal number
Here's the part of the codes where I tried to use boolean expression:

Code:

```#include <iostream> using namespace std; int main() {   int num;   cout << "8-bit Binary Number=";   cin >> num;     bool valid = true; . . . . . . . . . if (valid) {     cout << "Decimal Number = " << sum;   } }```
May I know that how can I get started with the body?

• 07-05-2013
sonjared
Quote:

May I know that how can I get started with the body?
The expected behavior is not clear. Please provide input and corresponding output examples.
• 07-05-2013
Elysia
• 07-05-2013
rcgldr
Note the default for cin and cout is to input and output decimal strings, while the numbers are stored internally as binary numbers. So it appears that your program will input a decimal string into a binary number, then convert the number as if the input was a binary string. You'll have to assume the user only input the characters '0' and '1' for the number. The program could input a string, then convert that string into a number.
• 07-05-2013
HTHVampire
Hi guys, thanks for your replies!
These are the output expected from me,
case 1:
8-bit Binary Number= 11111111
Decimal Number = 255

case2:
8-bit Binary Number=10101010
Decimal Number = 170

case 3:
8-bit Binary Number=101010102
Number entered is not a binary number
• 07-05-2013
rcgldr
The program should input a string.
• 07-06-2013
La Lala
Hi there, I bet you're an IT student from MMU .Right? =P
• 07-06-2013
yongsheng94
i Bet it too :)