Roman characters look very formal. I, II, III, IV … BBC uses Roman for their copyright statement (e.g. BBC (c) MMVI). And it looks exceptional. What is your birth year in Roman? Let your program find that for you! Here’s how:
We can do this through a strutils
function named: IntToRoman
The Syntax is as follows:
1 | function IntToRoman( |
Quick Tutorial
Create a new Application Project. Project -> New Project -> Application -> OK.
Drop a TEdit, TButton and a TLabel. You can name the controls appropriately, but for the sake of this tutorial let’s assume you named the TLabel as lblRoman. Double click the button and enter:
1 | lblRoman.Caption := IntToRoman(StrToInt(Edit1.Text)); |
Now Run the Project (F9 or Run -> Run). Enter a number such as 3
, 23
, 2013
on the editbox and click the button. You will see that the program works fine and returns the Roman equivalent.
But if you Enter characters, such as abc, the program will crash with the message below:
Click Break. Now get back to the code.
The problem with the code is that IntToRoman
accepts only Integer
s as inputs. And we have used StrToInt
function to convert user’s input into integer. But the function cannot convert strings containing any non-numeric character (character which is not a number). So we will need to detect if the user has entered integer or there is non-numeric character. We can use a function named Val
for this detection. The function has the following syntax:
1 | procedure Val( |
In simple words, it takes a string (S
) tries to convert it to integer and store it in a variable (V
). If failed, it returns the position where the non-numeric character appeared (thus returns a non-zero value in Code).
So we can modify the code as:
1 | procedure TForm1.Button1Click(Sender: TObject); |
Now Run again and try with a non-numeric character and the Label will show “(Error)”. Decorate further with adding a label for instruction and may be changing the font size and name. Have fun.
Photo: mommymaestra.com
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: