Is there a way for the user to input a binary number in C or C++?
If we write something like
int a = 0b1010;
std::cout << a << std::endl
To take binary number as input, there are two ways I use frequently:
(Key note: Take the input as string!!! use: #include)
to_ulong() method of the bitset template of the bitset library
bitset library using #includeExample :
string s;
cin>>s; // Suppose s = "100100101"
int n = (int) bitset<64>(s).to_ulong();
cout<
Explore more about
bitsethere and aboutto_ulong()here.
stoi() method of the string library
string library using #includeExample :
string s;
cin>>s; // Suppose s = "100100101"
int n = stoi(s, 0, 2);
cout<
Explore the format of
stoi()here.