Code:
program EX10DATEVALIDATIONSG(input,output);
uses crt;
const Title:string = ' EX10 DATE VALIDATION ';
By:string = ' By Steve Green ';
var dayNo,monthNo,yearNo:integer; //global
maxdayNo:integer; //within case & global
monthnames: array[1..12] of string = ('January','February','March','April',
'May','June','July','August','September','October','November','December');
procedure menu;
begin
ClrScr;
textcolor(red);
writeln(Title);
writeln;
writeln(by);
writeln;
textcolor(green);
writeln;
writeln('Hello, My name is Menu.');
writeln('I will try and validate any date that you type in.');
writeln('The range of years is between 1913 and 2012.');
writeln;
end;
procedure cont;
begin
writeln;
writeln;
writeln;
textcolor(red);
writeln ('Press <Return> to continue');
readln ();
end;
procedure monthMaxDay;
begin
case monthNo of {This section gives the}
4,6,9,11: maxdayNo := 30; {months their correct lengths}
1,3,5,7,8,10,12: maxdayNo := 31;
2: if yearNo mod 4=0 then maxdayNo :=29
else maxdayNo := 28; {leap yr validation, No need for}
end; {div100 etc as only 100yrs to be}
end; {checked}
procedure dayPrefix;
begin
case dayNo of
1,21,31: write (' ',dayNo,'st,'); {sets prefix for day}
2,22: write (' ',dayNo,'nd,');
3,23: write (' ',dayNo,'rd,');
4..20: write (' ',dayNo,'th,');
24..30: write (' ',dayNo,'th,');
end;
end;
begin {main program}
//BEGIN MENU,INSTRUCTIONS AND USER INPUT
menu;
writeln('Please enter the date to continue, enter in this format, DD MM YY.');
writeln('If the input is in any other format an error may be produced,');
writeln('please use DD MM YY. For example 22 05 83.');
writeln;
readln(dayNo,monthNo,yearNo);
//END OF USER INPUT
//ERROR HANDLING FOR MONTHS
if (monthNo <=0) or (monthNo >12) then
begin
repeat
writeln('Sorry, There are only 12 months in a year!');
writeln('Please enter the month again in MM format');
read(monthNo);
until (monthNo <=12) and (monthNo >0)
end;
if (monthNo > 0) and (monthNo <= 12) then
monthMaxDay;
// END OF MONTH CHECKING
//BEGIN ERROR HANDLING FOR DAYS WITHIN A MONTH
if (dayNo> maxdayNo) or (dayNo <=0) then
begin
repeat
writeln(monthnames[monthNo],' : There are only ',maxdayNo, ' days in this month!');
writeln('Please select a valid day');
read(dayNo);
until (dayNo <= maxdayNo) and (dayNo >0);
end;
write(monthnames[monthNo]);
dayPrefix; {needed now after day is ok}
//END OF DAY CHECKING
//YEAR CHECKING MAY NOT BE NEEDED AS RANGE WLL BE STIPULATED IN MENU.
//YEAR CHECKING
if (yearNo <=99) and(yearNo >12) then
write(' 19',yearNo);
if (yearNo <=09) and (yearNo >=00) then
write(' 200',yearNo);
if (yearNo >=10) and (yearNo <=12) then
write(' 20',yearNo);
//END OF YEAR CHECKING
writeln;
writeln;
cont;
end.
Bookmarks