Just as an additional help, you aren't really being asked to input binary anything.
You are being asked to convert a
string (like "1010") to an integer (10).
So, when you ask for input, just read a string:
1 2 3 4 5
|
string s;
cout << "Please enter a number in binary representation> ";
getline( cin, s );
// now to convert s to an int
|
You can validate the input on before converting, if you want, by just running through the characters in the string and complaining if any of them are not '0' or '1'.
Remember, to convert, you are adding powers of 2.
For example, 1010 is
1*23 + 0*22 + 1*21 + 0*20
You can do that in a loop. Start with a result of zero (no digits read).
For each digit, starting from the left ('0' or '1'):
- multiply your result by 2 (power up!)
- add 0 or 1 depending on whether the digit is '0' or '1'
You'll need to work this out in your head before it makes sense.
Good luck!