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.
kamalashraf 0 Newbie Poster
Recommended Answers
Jump to PostWell you can take the number in as a string and make sure that there is only 1's and 0's in it.
Jump to PostLook 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.
All 6 Replies
NathanOliver 429 Veteran Poster Featured Poster
kamalashraf 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
kamalashraf 0 Newbie Poster
Moschops 683 Practically a Master Poster Featured Poster
Banfa 597 Posting Pro Featured Poster
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.