Write a complete, well-written, documented program (named convert) that prompts for and reads in a number with at most MAX_DIGITS (9) in one of the following four formats: binary, octal, decimal, and hexadecimal and converts it to a requested base.
Once the number has been read in, you should verify that the number is valid (more on this later). If it is not valid, you should print the appropriate error message described below and exit with the specified error code immediately. After you determine the input is valid, you should determine what format the number is in (based on the rules described below) and display the appropriate type. Afterwards, the program should prompt for the base that the number should be converted to. You should verify that the base is within the range MIN_BASE and MAX_BASE (inclusive). If not, you should print the error message and exit with the exit code described below. Finally, the program output the number in the entered base and asks you if you want to continue. If you enter the value – as an input the program exits. Otherwise, it repeats the above steps
Input Format:
The input format for each of the four required types is as follows:
BINARY numbers will be prefixed with a capital B followed by 1-8 digits from 0-1.
OCTAL numbers will begin with '0' (zero) and be followed by 1-8 digits from 0-7.
DECIMAL numbers cannot begin with a '0' (zero) and must be followed by 1-9 digits from 0-9.
HEXADECIMAL numbers must begin with “0x” or “0X” followed by 1-7 digits from 0-9 and A-F.
The base will be input as an integer, as in '2' for binary, '8' for octal, '10' for decimal, '16' for hex
You must support all bases from MIN_BASE (2) up through MAX_BASE (36).
All input will be >= 0.
Input is limited to a total of MAX_DIGITS (9) in length.
Output Format:
Your program must be able to convert any of the above types into a decimal using the to_dec function described below. Once converted, the number must be printable in any base from MIN_BASE – MAX_BASE (inclusive) using the function print_dec_base described below.
beginner at c++ 0 Newbie Poster
vmanes 1,165 Posting Virtuoso
beginner at c++ 0 Newbie Poster
kenjutsuka 10 Light Poster
beginner at c++ 0 Newbie Poster
Labdabeta 182 Posting Pro in Training 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.