Re: stuck on my assignment (very beginner)

Posted by Jenny on March 14, 2005

In Reply to: stuck on my assignment (very beginner) posted by Glenn Stewart on March 14, 2005

: Design an algorithm that will prompt for and receive the time expressed in 2400 format (eg 2305 hours), convert it to 12 hour format (eg 11:05 pm.) and display the new time on the screen. Your program is to repeat the processing until six times have been entered, processed and displayed.

: Execution of the program may proceed as follows:

: Enter time to convert in 24 hour format -> 2305
: 2305 is equivalent to 11:05 pm.
: Enter time to convert in 24 hour format -> 925
: 0925 is equivalent to 9:25 am.
: Enter time to convert in 24 hour format -> 1200
: 1200 is equivalent to 12:00 pm. Note 12 midday is pm
: Enter time to convert in 24 hour format -> 5
: 0005 is equivalent to 12:05 am.
: ….and so on until 6 times have been processed.

For the conversion from 2400 format to am/pm you can use this function:

function Time2400ToAmPm(T: string): string;
var
   H, M, AmPm: string;
begin
  H := Copy(T, 1, 2);
  M := Copy(T, 3, 2);
  if StrToInt(H) > 12 then begin
    AmPm := 'pm';
    H := IntToStr(StrToInt(H) - 12); 
    if Length(H) = 1 then
      H := '0' + H;
  end
  else
    AmPm := 'am';
  Result := H + ':' + M + ' ' + AmPm;
end;     

For the rest, I'm not gonna do your homework. You should look at a good Delphi tutorial ;) for example, at this very site ;)
Jenny

Related Articles and Replies