Re: Dos application HELP ME PLEASE!!!!!!

[ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by webmaster Guido on December 07, 2001:

In Reply to: Dos application HELP ME PLEASE!!!!!! posted by Kees on December 05, 2001:

: I want to make a dos application for school. The progamm has to do the following thing: input = yyyymmdd after the input he has to calculate how many day's are left of the 365.


Let's have a look how you would do this in a Delphi application for Windows -- don't forget that Delphi is primarily a RAD-tool for *Windows*. At the end, I'll give some tips for DOS.

What must our program do?

1. Try to extract the year, month and day from the input value.
2. Try to convert the year, month and day to a TDate value.
3a. If we obtained a valid date:
- generate the last date for that year;
- calculate the difference between the two dates: substract the integer parts from each other;
- convert the difference into a string and display it.
3b. If there was an error, warn the user that the input was invalid.

We start a new application. On its main (and only) form, let's drop the following components:
- a TEdit box for the input; in the Object Inspector, change its name to edDate and its text property to some initial value, say 20010101;
- a TButton, call it btnCalculate and set its caption to anything you wish;
- a TLabel for the output, call it lblDaysLeft and enter ???? as the initial value for its caption.

Next, we generate an OnClick event-handler for the button. Just doubleclick on the button and Delphi will write the skeleton for the event handler in the editor. Let's complete the event handler as follows:

procedure TForm1.btnCalculateClick(Sender: TObject);
  i, Year, Month, Day, DaysLeft: integer;
  DateStart, DateEnd: TDate;
    if Length(edDate.Text) <> 8 then
      // We raise an exception if the length is not correct
      // Then, the program jumps to the line "except"
      raise ERangeError.Create('Length must be 8 characters');
    // If there is a conversion error in any of the next lines,
    // the program also jumps to the line "except"
    Year := StrToInt(Copy(edDate.Text, 1, 4));
    Month := StrToInt(Copy(edDate.Text, 5, 2));
    Day := StrToInt(Copy(edDate.Text, 7, 2));
    DateStart := EncodeDate(Year, Month, Day);
    // Only if all goes well, we reach the next line
    DateEnd := EncodeDate(Year, 12, 31);
    DaysLeft := Trunc(DateEnd) - Trunc(DateStart);
    lblDaysLeft.Caption := IntToStr(DaysLeft);
    // The exception we raised ourselves:
    on ERangeError do     
      ShowMessage('Invalid input. Length must be 8 characters.');
    // ...or an exception raised by Delphi:
    on EConvertError do         
      ShowMessage('Invalid input. Enter a valid date in the format YYYYMMDD.');

Note that if an "exception" occurs when the program is run in the IDE, we see TWO error messages. When the program is run outside of Delphi's IDE, e.g. by double-clicking its exe-file, we only see the second error message (one of the messages that we programmed after the line "except").

Well, for a DOS program, you would have to convert this to a "console" application. Because in DOS you don't have edit-boxes, buttons nor labels, you must input a value from the "console" (command line), do the calculations, output messages to the console if necessary, and finally display the result on the console. Tip: don't forget to wait for some input from the user that terminates the program or that initiates a new calcaculation, because you must give the user time to read the messages!

I'm not going into details here, because DelphiLand publishes tutorials for *beginners* ;-)
Anyway, I hope that this short intro points you into the right direction, and you'll succeed with some extra research about "console applications" (see: the manuals, the web, a good Delphi programming book).

[ DelphiLand Discussion Forum ]