Daily Archives: April 10, 2012

C++ || Class – Roman Numeral To Integer & Integer To Roman Numeral Conversion


 

Click Here For Updated Version Of Program


The following is another homework assignment which was presented in a C++ Data Structures course. This program was assigned in order to practice the use of the class data structure, which is very similar to the struct data structure.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Header Files - How To Use Them
Class - What Is It?
Do/While Loop
Passing a Value By Reference
Roman Numerals - How Do You Convert To Decimal?
Online Roman Numeral Converter - Check For Correct Results

This is an interactive program in which the user has the option of selecting from 7 modes of operation. Of those modes, the user has the option of entering in roman numerals for conversion, entering in decimal numbers for conversion, displaying the recently entered number, and finally converting between roman or decimal values.

A sample of the menu is as followed:
(Where the user would enter numbers 1-7 to select a choice)


This program was implemented into 3 different files (two .cpp files, and one header file .h). So the code for this program will be broken up into 3 sections, the main file (.cpp), the header file (.h), and the implementation of the functions within the header file (.cpp).

======== FILE #1 – Menu.cpp ========

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.



======== FILE #2 – ClassRomanType.h ========

Remember, you need to name the header file the same as the #include from the Menu.cpp file. This file contains the function declarations, but no implementation of those functions takes place here.


======== FILE #3 – RomanType.cpp ========

This is the function implementation file for the ClassRomanType.h class. This file can be named anything you wish as long as you #include “ClassRomanType.h”


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 9

You selected choice #9 which will:
Error, you entered an invalid command!
Please try again...
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 0
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: Currently Undefined
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 1

You selected choice #1 which will: Get Decimal Number

Enter a Decimal Number: 1987
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 1987
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 3

You selected choice #3 which will: Convert from Decimal to Roman
Running....
Complete!
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: MCMLXXXVII
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 2

You selected choice #2 which will: Get Roman Numeral

Enter a Roman Numeral: mMxiI
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: mMxiI
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 4

You selected choice #4 which will: Convert from Roman to Decimal
Running....
Complete!
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 2012
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 7

You selected choice #7 which will: Quit
--------------------------------------------------------------------
Bye!