Re: Dos application HELP ME PLEASE!!!!!!
Posted by webmaster Guido on December 07, 2001:
In Reply to: Dos application HELP ME PLEASE!!!!!! posted by Kees on December 05, 2001 at 12:14:35:
: 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.
1. Try to extract the year, month and day from the input value.
We start a new application. On its main (and only) form, let's drop the following components:
procedure TForm1.btnCalculateClick(Sender: TObject); var i, Year, Month, Day, DaysLeft: integer; DateStart, DateEnd: TDate; begin try 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); except // 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.'); end; end;
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* ;-)