/*************************************************
Reading and adding binary numbers
Write a program that reads in two numbers in
binary, and writes out the sum of the numbers
as an int. Write a function:
int readbinary(istream&);
that is able to read a binary number in from any
istream object - i.e. from cin or from a file
input stream - and which returns the int value
of the binary number read in.
*************************************************/
**#include <iostream>**
**#include <fstream>**
**using** **namespace** std;
**int** readbinary(istream&);
/************************************
** main()
************************************/
**int** main()
**{**
**int** a, b;
cout << "Enter a binary number: ";
a = readbinary(cin);
cout << "Enter a binary number: ";
b = readbinary(cin);
cout << "The sum is " << a + b << endl;
**return** 0;
**}**
/************************************
** readbinary(IN) Reads in a binary
** number from istream IN and returns
** the int value of the number read.
************************************/
**int** readbinary(istream& IN)
**{**
// Read in first bit (and skip whitespace!)
**char** c;
IN >> c;
**int** total = c - '0';
// Read in subsequent bits
c = IN.get();
**while**(c == '0' || c == '1')
**{**
total = 2*total + (c - '0');
c = IN.get();
**}**
// Return value of binary number
**return** total;
**}**