Description
Write a C++ program that converts numbers from and to different number
systems(bases). For this end your program:
(a) displays a menu to the user and asks the user to choose the source number
system from the menu by inputting an integer value. The source number
system is a member of set R = {2, 3, 4, 5, 6, 7, 8, 9, 10, 16}. If the user input
value is a member of R, an inner menu (blue box shown in section c) will
be displayed. Otherwise, the program displays a message that shows the
user decided to quit the program and ask for confirmation (Y/N). Here is
how the main menu of the program looks like:
The main menu of the program
=====================================
Number System Conversion======================
=====================================
CHOOSE YOUR SOURCE NUMBER SYSTEM:
(2) BINARY
(3) TERNARY
(4) QUATERNARY
(5) QUINARY
(6) SENARY
(7) SEPTENARY
(8) OCTAL
(9) NONARY
(10) DECIMAL
(16) HEXADECIMAL
INPUT ANY OTHER VALUE TO EXIT THE PROGRAM.
=====================================
(b) when the user input value is NOT a member of R, the following menu will
be displayed.
1
Invalid Input
=====================================
Confirm Close =============================
=====================================
ARE YOU SURE YOU WANT TO QUIT THE PROGRAM?(Y/N)
=====================================
When the user chooses Y, the program outputs ”BYE!” and terminates.
Otherwise, the user will be returned to the main menu after displaying
this message ”You will be returned to the main menu”.
Invalid Input/Y
=====================================
Confirm Close =============================
=====================================
ARE YOU SURE YOU WANT TO QUIT THE PROGRAM?(Y/N)
Y
BYE!
=====================================
Invalid Input/N
=====================================
Confirm Close =============================
=====================================
ARE YOU SURE YOU WANT TO QUIT THE PROGRAM?(Y/N)
N
YOU WILL BE RETURNED TO THE MAIN MENU.
=====================================
(c) when user inputs 2, 3, 4, 5, 6, 7, 8, or 9, a second menu(blue box) displays
and asks the user to choose the target number system. For instance, when
the user inputs 8, the following menu displays: (you should represent the
source number system at the top of this menu as shown)
2
Input ∈ {2, 3, 4, 5, 6, 7, 8, 9}
=====================================
Source Number System : 8 =======================
=====================================
CHOOSE YOUR TARGET NUMBER SYSTEM:
(10) DECIMAL
INPUT ANY OTHER VALUE TO QUIT THE PROGRAM.
=====================================
In this step, if the user inputs 10, the program
(i) reads the user input number as a string (also print the source number system),
(ii) converts the number to decimal,
(iii) outputs the result,
(iv) displays the ”You will be returned to the main menu” message,
(v) and returns to the main menu.
If the user inputs any other value, the program displays the red box in
section c to confirm that the user wants to exit, etc.
Conversion Menu
=====================================
Source Number System : 8, Target Number System: 10 ======
=====================================
INPUT YOUR NUMBER IN SOURCE NUMBER SYSTEM:
OCTAL: 234
DECIMAL: 156
YOU WILL BE RETURNED TO THE MAIN MENU.
=====================================
(d) when the user inputs 10, the program displays the following menu and
asks the user to choose a target number system.
3
Input ∈ {10}
=====================================
Source Number System: 10 =====================
=====================================
CHOOSE YOUR TARGET NUMBER SYSTEM:
(2) BINARY
(3) TERNARY
(4) QUATERNARY
(5) QUINARY
(6) SENARY
(7) SEPTENARY
(8) OCTAL
(9) NONARY
(16) HEXADECIMAL
INPUT ANY OTHER VALUE TO QUIT THE PROGRAM.
=====================================
In this step, if the user inputs a valid value, the program
(i) reads the user input number as a string,
(ii) outputs its equivalent in the target number system,
(iii) and finally returns to the main menu.
If the user inputs any other value, the program displays the red box in
section c to confirm that the user wants to exit, etc.
Conversion Menu
=====================================
Source Number System: 10, Target Number System: 3 ======
=====================================
INPUT YOUR NUMBER IN SOURCE BASE:
DECIMAL: 345
TERNARY: 110210
YOU WILL BE RETURNED TO THE MAIN MENU.
=====================================
(e) when user inputs 16, the program displays the following menu and asks
the user to choose the target number system.
4
Input ∈ {16}
=====================================
Source Number System: 16 ======================
=====================================
CHOOSE YOUR TARGET NUMBER SYSTEM:
(8) OCTAL
(10) DECIMAL
INPUT ANY OTHER VALUE TO QUIT THE PROGRAM.
=====================================
In this step, if the user inputs 8 or 10, the program
(i) reads the user input number as a string,
(ii) and validates the input using an input validation loop. As a result,
if the user-input number is not a valid hexadecimal number the program asks for another input. The program gives the user 5 chances,
if the user inputs 5 invalid hexadecimal numbers the program returns
to the main menu with the following message.
Invalid Hex Number
=====================================
Source Number System: 16, Target Number System: 8 ======
=====================================
INPUT YOUR NUMBER IN SOURCE BASE:
HEXADECIMAL: 12M
NOT A VALID NUMBER! TRY AGAIN:
HEXADECIMAL: 6Q45
NOT A VALID NUMBER! TRY AGAIN:
HEXADECIMAL: S536
NOT A VALID NUMBER! TRY AGAIN:
HEXADECIMAL: 72Y1
NOT A VALID NUMBER! TRY AGAIN:
HEXADECIMAL: 536T
NOT A VALID NUMBER!
YOU WILL BE RETURNED TO THE MAIN MENU.
=====================================
When the user inputs a valid hexadecimal number, the program outputs
its octal or decimal equivalent based on the chosen target number system.
Finally, it displays the main menu again. If the user inputs any other
value, the program displays the red box in section c to confirm that the
user wants to exit, etc.
5
Conversion Menu
=====================================
Source Number System: 16, Target Number System: 8 ======
=====================================
INPUT YOUR NUMBER IN SOURCE NUMBER SYSTEM:
HEXADECIMAL: 1BC
OCTAL: 674
YOU WILL BE RETURNED TO THE MAIN MENU.
=====================================
INSTRUCTIONS
1. The best approach would be to implement this code incrementally. Otherwise debugging the code could get very hard. Implement a small step
at a time, test your code for yourself to make sure it works correctly, and
then go to the next step.
2. Implement the main menu branches with switch, NOT if-else.
3. The hexadecimal to octal conversion should be through base 2 as
explained in this video. You won’t get any point for converting to 10
and then 8. If you use any reference for this part of the problem, do not
forget to cite them. As I mentioned before, you should NOT search for
the code but you can search for the algorithm to see how this conversion is
supposed to be done. Your reference should match your implementation.
4. Make your program as concise as possible. Generalize the base conversions
similar to what I showed in the Binary to Decimal Conversion demo and
just set the base based on the user inputs.
5. For input validation loop, check the cctype library functions and choose
the most relevant one. You won’t get any point for writing an if-statement
with logical or of 16 conditions.
6. For submission, you should try all the possible different scenarios to see
different menus of your program, try with different input values and get
screenshots.
7. If you are unsure about any part of the problem, do NOT make assumptions, ask instead.
6