i am making a number base conversion program in c++,
suppose that user enters an base 2 number and want to convert that number in base 4. when he enter the number (which he wants to be converted), how can i detect that number is belongs to base 2 number system or not.
for example: if user enter 1001011001 , program should continue and convert that number to base 4. and if user enter 1200101 or 1001012 , program should not convert that number and output that this number is not belongs to base 2 number system. waiting for your kind reply. thanks.

Well you can take the number in as a string and make sure that there is only 1's and 0's in it.

please explain your answer. thanks.

If you only need to determine whether a number could be in a certain base, just check the digits. If there are any illegal digits for the base, you can exclude that base.

If you need to say conclusively that something like 1001011001 is binary, it's much harder. That particular number is perfectly valid for any base, so unless you're making assumptions about what constitutes a binary value, you're SOL. With some reasonable assumptions that limit what each base can represent, you can introduce heuristics, but there will still be false positives or false negatives for degenerate input that go against your assumptions.

So...what are your allowed assumptions?

i am just new to c++, just tell me one thing that how can i check that all digits of a number are less than 2.

Look at the first digit. Check if it is less than two.
Look at the next digit. Check if it is less than two.
Repeat for all digits.

I suggest you get the number as a C++ string, and examine each char in the string using the [] operator.

<pedantic>

If you need to say conclusively that something like 1001011001 is binary, it's much harder. That particular number is perfectly valid for any base

Strictly speaking that number is not valid in base 1 which is a valid base which has a single arbitary digit (normally 1 is used). Since this number uses 2 digits 0 and 1 it can not be a base 1 number.
</pedantic>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.