First Steps with Free Pascal – The currency converter

Introduction:
I was lately blessed with an array of coursework assignments for a module called “Paradigms of Programming”. The language I picked to get into imperative programming was Pascal and I had a few fun weekend with it programming a currency converter.
I’m hoping others might find the resulting code helpful or interesting :)
Preparations:

 

The Code – Stage 1: Source Currency
Step 1: Constants and variables
We need to initialising a few constants for the currency symbols
const
euro = ‘€’;
pound = ‘£’;
dollar = ‘$’;

And now we initialise some variables to hold temporary results of each step
var
step_one : integer;
step_one_cur : string;
step_two : real;
step_three : integer;
step_three_cur : string;

Step 2: Let’s get the user to enter the source currency
writeln (‘Welcome to the exciting world of Pascal currency conversion!’);
writeln (‘This program will convert between the following currencies for you:’);
writeln (euro, ‘,’, pound, ‘,’, dollar);
writeln (‘——————————————————-’);
writeln (‘Step 1 – Please pick the currency of which you have notes/coins from the following list by entering 1,2 or 3 and hitting enter:’);
writeln (’1: ‘, euro);
writeln (’2: ‘, pound);
writeln (’3: ‘, dollar);
readln (step_one);

Step 3: To reassure our user we tell him what we think he just entered for step one but end the program if the user picks anything apart from 1-3
case step_one of
1:
begin
step_one_cur := euro;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
2:
begin
step_one_cur := pound;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
3:
begin
step_one_cur := dollar;
writeln (‘You have selected ‘, step_one_cur, ‘ as your source currency.’)
end;
otherwise writeln (‘Only options 1-3 are available for Step 1, please try again.’);
exit;
end;

 

The Code – Stage 2: Source amount
Step 1: Let’s get the user to enter the source amount but end the program if the user enters a negative amount
writeln (‘——————————————————-’);
writeln (‘Step 2 – Please enter the amount of ‘, step_one_cur, ‘s you have and press enter:’);
readln (step_two);
{end program if the user enters a negative source amount}
if step_two < 0 then
begin
writeln (‘You have entered a negative amount of ‘, step_two:3:2, step_one_cur, ‘, please try again’);
exit;
end;

Step 2: To reassure our user we tell him what we think he just entered for step two
writeln (‘You have entered: ‘, step_two:3:2, step_one_cur);

 

The Code – Stage 3: Target currency

Step 1: Let’s get the user to enter the target currency but end the program if the user enters the same currency we have from Stage 1 as source currency or picks outside the 1-3 range
writeln (‘——————————————————-’);
writeln (‘Step 3 – Please pick the currency you want your ‘, step_two:3:2, step_one_cur, ‘ converted to by entering 1,2 or 3 and hitting enter:’);
writeln (’1: ‘, euro);
writeln (’2: ‘, pound);
writeln (’3: ‘, dollar);
readln (step_three);

if step_one = step_three then
begin
writeln (‘You have picked the same currency for step 1 and 3, please try again’);
halt;
end;

Step 2: To reassure our user we tell him what we think he just entered for step three}
case step_three of
1:
begin
step_three_cur := euro;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
2:
begin
step_three_cur := pound;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
3:
begin
step_three_cur := dollar;
writeln (‘You have selected ‘, step_three_cur, ‘ as your target currency.’)
end;
otherwise writeln (‘Only options 1-3 are available for Step 1, please try again.’);
exit;
end;

 

The Code – Stage 4: Let’s do some converting and feed source amount, source currency and target currency from the previous steps into the step four procedure
writeln (‘——————————————————-’);
Step 1: To reassure our user we tell him what we think he entered for step 1-3
writeln (‘Step 4 – If everything went well you want ‘, step_two:3:2, step_one_cur, ‘ converted to ‘, step_three_cur);

Step 2: Feed step_four procedure
step_four (step_one_cur, step_two, step_three_cur)
end.

 

The Code – Stage 5: Calculate the result

Step 1: Definition, constants and variables
First we define what our step_four procedure get’s fed with
procedure step_four (source_cur : string; source_amount : real; target_cur : string);
Then we initialise the variables for the currency symbols
var euro : string;
var pound : string;
var dollar : string;
Initialise variables for the conversion rates
var euro2pound : real;
var euro2dollar : real;
var pound2euro : real;
var pound2dollar : real;
var dollar2euro : real;
var dollar2pound : real;
Initialise the temp variable which will hold the final result for step five
var step5 : real;

Step 2: Fill variables
We fill the variables we initialised above with currency symbols, conversion rates and set step five temp variable to zero
euro := ‘€’;
pound := ‘£’;
dollar := ‘$’;
euro2pound := 0.8;
euro2dollar := 1.3;
pound2euro := 1.2;
pound2dollar := 1.6;
dollar2euro := 0.8;
dollar2pound := 0.6;
step5 := 0;

Step 3: Picking the right conversion rate
Depending on source and target currency we tell the user the conversion rate and assign the result to our step five variable
if (source_cur = euro) and (target_cur = pound) then
begin
writeln (‘The exchange rate for euro to pound is: ‘, euro2pound:3:2);
step5 := source_amount*euro2pound
end;

if (source_cur = euro) and (target_cur = dollar) then
begin
writeln (‘The exchange rate for euro to dollar is: ‘, euro2dollar:3:2);
step5 := source_amount*euro2dollar
end;

if (source_cur = pound) and (target_cur = euro) then
begin
writeln (‘The exchange rate for pound to euro is: ‘, pound2euro:3:2);
step5 := source_amount*pound2euro
end;

if (source_cur = pound) and (target_cur = dollar) then
begin
writeln (‘The exchange rate for pound to dollar is: ‘, pound2dollar:3:2);
step5 := source_amount*pound2dollar
end;

if (source_cur = dollar) and (target_cur = euro) then
begin
writeln (‘The exchange rate for dollar to euro is: ‘, dollar2euro:3:2);
step5 := source_amount*dollar2euro
end;

if (source_cur = dollar) and (target_cur = pound) then
begin
writeln (‘The exchange rate for dollar to pound is: ‘, dollar2pound:3:2);
step5 := source_amount*dollar2pound
end;

 

The Code – Stage 6: Spit out the whole lot

Let’s tell our user how much X of Y he would get for his A of B at the current conversion rate of C}
writeln (‘——————————————————-’);
writeln (‘Step 5 – Results:’);
writeln (‘At the current exchange rate you would get ‘, step5:3:2, target_cur, ‘ for your ‘, source_amount:3:2, source_cur);

Pascal Currency Converter

 

 

Conclusion:
Yes, I admit growing up with Pascal and C so this brings back fond memories of sitting in a dark cellar computer lab in a school in Munich :) I can recommend Pascal to anyone who wants to get into imperative programming, it’s easy enough to pick up and once one hit’s it’s limitations it’s easy to move on to mighty old C.

 

 

 

 

 

Thanks for rating this! Now tell the world how you feel - .
How does this post make you feel?
  • Excited
  • Fascinated
  • Amused
  • Bored
  • Sad
  • Angry
FacebookGoogle GmailEvernoteDeliciousShare

Add a comment »2 comments to this article

  1. I would say one word for this article simply awesome. Thanks for giving me such a clear technical scope of work

    Reply

    • Very happy somebody else found this useful, it was good fun to write :)

      Reply


*

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress


Hit Counter provided by seo company