There are plenty of ways to read the eight (?) characters that you want. Discarding everything after that is equally varied. Two fairly common ones that don't use an explicit loop are:
Code:
#include <limits>
cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
and
Code:
cin.ignore ( cin.rdbuf()->in_avail() );
A quick implementation is obvious once you have those:
Code:
#include <iostream>
#include <limits>
using namespace std;
int bit_convert ( char bits[] )
{
int dec = 0;
int base = 1;
for ( int i = 7; i >= 0; i-- ) {
if ( bits[i] != '0' )
dec += base;
base <<= 1;
}
return dec;
}
int main()
{
char bits[9];
cout<<"Enter an octet: ";
if ( cin.getline ( bits, sizeof bits ) ) {
cin.ignore ( cin.rdbuf()->in_avail() );
cout<<"The value of "<< bits <<" is "<< bit_convert ( bits ) <<endl;
}
}
Slightly harder is handling errors and realizing that a byte isn't always eight bits, then changing your code to deal with that.