I am working on a school project, I need to do a keystroke to simulate
filling car with petrol. There is two program I have done below but none
of them work. I use Borland developer studio 2006 (Delphi Pascal) and
does anyone know how to clear the screen in the console please. Can
anyone want help me or tell me what is wrong with my program please.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var

  start_time,stop_time,total_time:real;
  key:string;

procedure first_time;

begin
start_time := time;
end;

procedure secound_time;

begin
stop_time := time;
end;

begin

writeln('Press F to get start time');
readln(key);

writeln('Press S to get stop time');
readln(key);

writeln('Press A to get total time');
readln(key);

readln;
if key = 'f' then
(first_time) ;

if key = 's' then
(secound_time);

if key ='a' then
total_time := stop_time - start_time;
writeln(total_time);
readln;
end.
program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  total_time:word;
  start_time,start_min,start_sec,start_hour:word;
  stop_time,stop_hour,stop_min,stop_sec:word;
  hour,min,sec,msec:word;
  key:string;

begin

writeln('Press F to get start time');
readln(key);

writeln('Press S to get stop time');
readln(key);

writeln('Press A to get total time');
readln(key);


if key = 'f' then
begin
DecodeTime( time, hour, min, sec, msec );
start_hour:= hour *3600;
start_min:= min*60 ;
start_sec:=sec;
start_time:= start_hour + start_min + start_sec ;
end;

if key = 's' then
begin
DecodeTime( time, hour, min, sec, msec );
stop_hour:= hour *3600;
stop_min:= min*60 ;
stop_sec:=sec;
stop_time:= stop_hour + stop_min + stop_sec ;
end;

if key = 'a' then
total_time := stop_time - start_time;
writeln(total_time);
readln;

end.

Both are variations on the same thing.

First off, each time you [B]readln([/B]key[B]);[/B] you change the value of key. So if you ask for it three times, it will only be whatever you entered the third time.

I would recommend just forgetting the key and asking the user to press ENTER for each thing.

{$apptype console}
uses SysUtils, DateTime;
begin
writeln( 'Petrol Pumper' );

write( 'Press ENTER to start fueling ' );
readln;
start_time := time;

write( 'Press ENTER to stop fueling ' );
readln;
stop_time := time;

write( 'The total time you spent fueling is ' );
write( timeToStr( stop_time -start_time ) );
writeln( '.' );

end.

You will probably want to convert the time spent fueling into the amount of fuel dispensed, and then calculate a price?

Hope this makes things clearer for you.

Don't clear the screen for this type of console program. It isn't worth the trouble.

Thx for your help but I am using Borland Developer Studio 2006 so what variable type should I use to declare start_time and stop_time and use

uses SysUtils, DateTime;

don't work it come up as an error.

I have edit your program to work out the amount of petrol dispensed but still don't work I think is something to do with the datatype , can u tell me what is wrong with it please.

program Project1;

{$apptype console}
uses SysUtils;

var
start_time,stop_time,total_time:real;

begin
writeln( 'Petrol Pumper' );

writeln( 'Press ENTER to start fueling ' );
readln;
start_time := time;

writeln( 'Press ENTER to stop fueling ' );
readln;
stop_time := time;

writeln( 'The total time you spent fueling is ' );

writeln( ((stop_time -start_time)*0.25):5:2 );
writeln( '.' );
readln;
end.

My apologies, I use D5 which has all the date functions in the System unit. The correct unit you need is DateUtils. (You could have learned this by placing the cursor on the timeToStr function and pressing F1.) The compile errors are also very specific about exactly where the error occurs. Click on the error in the Message Window and press F1 for more detailed information.

You might also want to take a look at this thread: Tutorial: Handling Dates and Time in Delphi.

Hope this helps.

I have try to declare the start_time and stop_time with TDateTime but it come back with a very long number very letter e.g 2.154393438940E-0006 and the time showing is not correct other because I need to do calculation with the time so I can't use word variable. I have try to use the datauntil but it casue all the code error for some reason. Thx for your help again.

program Project1;

{$apptype console}

uses
SysUtils,DateUtils;

var
start_time,stop_time,total_time:Tdatetime;

begin
writeln( 'Petrol Pumper' );

writeln( 'Press ENTER to start fueling ' );
readln;
start_time := time;

writeln( 'Press ENTER to stop fueling ' );
readln;
stop_time := time;

writeln( 'The total time you spent fueling is ' );
writeln( timeTostr(stop_time -start_time) );
total_time:= stop_time - start_time;
writeln(total_time);
writeln( '.' );
readln;
end.

Wait a second or two before pressing ENTER again. If you want milliseconds, take a look at the DecodeTime procedure. (Please read the thread I posted.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.