Pascal-Programming.info

A step-by-step Pascal tutorial for beginners.

Uses of the CRT Functions

Before you learn how to use if statements and for loops, I would like to give you an idea of some pascal functions which are quite useful. These functions belong to the library (or 'unit') called CRT and thus this would require your program to include the library 'crt.tpu'. To include a library in the program, one should use the reserved word 'uses', because it is used to call a library of functions and procedures. Note that a reserved word is a word used in the syntax of programming language and cannot be used as an identifier such as variable name or function name. Here is the program of lesson 2 (program 3) which is better handled and more user-friendly:

Program lesson3_Program1;

Uses Crt; {We will make use of the crt library}

Var PD, Dname, Cmodel : String;
    CostPD, TCostPD, Distance : Real; 
    {real is a decimal (described later on)}

Begin
	textbackground(brown); {background colour}
	ClrScr; {Clear screen with a brown colour. Try run the program without this..}
	TextColor(lightgreen); {text colour}
	TCostPD := 0; 
	Writeln('This program prompts you to ' +
		+ 'input the cost per litre of');
	Writeln('the petrol/diesel you spend in and ' +
		+ 'the average distance you travel');
	Writeln('with your car every week. Then, ' +
		+ 'the computer calculates the total cost');
	Writeln('you spend in fuel every week.');
	Readkey; {program move on as soon as a key is pressed}
	ClrScr; {short for clear screen}
	GotoXy(28,3); {move to a position on the screen: x (horizontal), y (vertical)}
	Write('Diesel or Petrol? Type p or d: ');
	PD := Readkey; {as soon as a key is pressed, it is stored in the variable 'PD'}
	GotoXy(30,4);
	Write('Name Of Driver: ');
	Readln(Dname);
	GotoXy(30,5);
	Write('Car Model: ');
	Readln(Cmodel);
	GotoXy(29,6);
	Write('Cost of Diesel/Petrol: (£) ');
	Readln(CostPD);
	GotoXy(8,7);
	Writeln('Average distance you travel with ' +
		+ 'your car every week: (kilometres) ');
	Readln(Distance);
	ClrScr;
	GotoXy(28,3);
	Writeln('Name of Driver:',Dname);
	GotoXy(31,4); 
	Delay(500);
	Writeln('Car Model:',Cmodel);
	GotoXy(32,5); 
	Delay(500);
	Writeln('Diesel/Petrol:',PD);
	GotoXy(8,6); 
	Delay(500);
	Writeln('Average distance covered '+
		+ 'every week: ',Distance:1:2,'Km');
	GotoXy(25,7); 
	Delay(500);
	Writeln('Cost of ',PD,' per litre: £',CostPD:1:2,'/litre');
	Writeln; 
	Delay(500);
	Writeln;
	TCostPD := Distance * CostPD;
	GotoXy(21,10);
	Writeln('Total cost of ',PD,' per week:£',TCostPD:1:2);
	TCostPD := 0;
	GotoXy(21,12);
	Writeln('Total cost of ',PD,' per week:' +
		+ '£',(Distance * CostPD):1:2);
	GotoXy(18,14);
	Writeln('Total cost of ',PD,' per week:£',Distance * CostPD);
	Readln;
End.

(if you want to see the difference of the 2 programs then you should run them) What is the difference between this program and the program which is program 3 in lesson 2? The 'CRT' (short for cathode-ray tube) library has a wide range of functions and procedures that you will use very frequently. Some of them are listed in the table below. There are many similar libraries, such as 'Strings' (you will be learning something on this later on) and 'Dos'.

Description of the CRT Functions

Below is a table of the new words:

Reserved Word

Crt: Yes/No

Description

Clrscr Yes Clears screen
Gotoxy(int,int) Yes Takes the cursor to the pre-defined position
Textbackground(word/int) Yes Background colour
Textcolor(word/int) Yes Colour of text
Readkey Yes Reads a key; Could be assigned to a variable
Delay(int) Yes Waits for the included time(milliseconds)
Halt(parameter) No Program terminates

int - integer (-32768 to 32767), word - 0 to 65535.

Examples:

Clrscr: (clear screen)

Writeln('When you press enter, the screen would be cleared!');
Readln;
ClrScr;

Gotoxy(int,int): (Go to position x and y);

GotoXY(10,10);
Writeln('The position is 10 pixels from the left of the screen, and ten pixels');
Writeln('from the top of the screen.');
Readln;

Textbackground(word/int): (Background colour);

Textbackground(red); {word - red}
Writeln('Note the difference');
Textbackground(5); {integer - 5}
ClrScr;
Writeln('Note the difference'); Readln;

Textcolor(word/int): (Text colour);

Textcolor(red); {word - red}
Writeln('Text colour');
Textcolor(5); {integer - 5}
Writeln('Text colour'); Readln;

Readkey: (Reads a key-press);

Example 1:

Writeln('Press ANY key!!!');
Readkey;

Example 2:

Writeln('Press ANY key');
Keypress := Readkey; {keypress is a DECLARED string variable(can be an integer variable)}
Writeln(Keypress);

Delay(int): (Holds for some time in milliseconds);

Writeln('1');
Delay(1000); {1000 milliseconds}
Writeln('2');
Delay(1000);
Writeln('3');
Readln;

Halt(int): (Program terminates with an exit code);

Writeln('Press enter and the program terminates!);
Readln;
Halt(0);        

Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.

Spread The Word!

Have Your Say