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.
The valid digits for base 2 number are {0, 1}.
The valid digits for base 5 number are {0, 1, 2, 3, 4}.
The valid digits for base 42 number are {0, 1, .. ?}.
You could therefore std::find a character from the string with a comparison functor that returns true, if digit is not valid.
how can i detect that number is belongs to base 2 number system or not
Ask the user. There is no way to determine whether a particular string of digits was meant to be a binary representation or a decimal/octal/hex representation if it consists only of digits which are common to all of them just by inspecting it.