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
month:string;
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 am here to make things easier');
writeln('To exit the program press 0');
writeln; //seperate procedure?
writeln;
end;
procedure cont;
begin
writeln;
writeln;
writeln;
textcolor(red);
writeln ('Press <Return> to continue');
readln ();
end;
procedure monthcheck;
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
end;
end;
procedure daycheck;
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,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,24,
25,26,27,28,29,30: write (dayNo,'th');
end;
end;
begin {main program}
menu; //can maybe have the input as menu / sep proc?
write('Please enter the date to continue, enter in this format, DD MM YY.');
writeln;
readln(dayNo,monthNo,yearNo);
//END OF USER INPUT
//BEGIN ERROR HANDLING FOR DAYS WITHIN A MONTH
monthcheck;
if (dayNo> maxdayNo) or (dayNo <=0) then
begin
writeln('There are ',maxdayNo, ' of days in this month!');
writeln('Please select a valid day');
read(dayNo);
end;
if (dayNo>=1) or (dayNo<=31) then
daycheck;
//ERROR HANDLING FOR MONTHS
if (monthNo <=0) or (monthNo >12) then
begin
writeln('Sorry, There are only 12 months in a year!');
writeln('Please enter the month again');
read(monthNo);
end;
if (monthNo > 0) and (monthNo <= 12) then
monthcheck;
write(' ',monthnames[monthNo]);
//NEED ERROR HANDLING FOR YEARS (OUTSIDE OF RANGE ETC)
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);
writeln;
writeln;
cont;
end.
Thanks
Bookmarks