PDA

View Full Version : Any improvements?



stevengreen22
29-04-2011, 11:55 AM
Hi guys,

the date program I was working on is up and running and doing what I want it to do...ina nut shell anyway.

Tajes a date in 6 digit format, error checks days in months etc and then outputs as month name, day no and prefix and year in four digits.

Can anyone suggest any improvements? It was mentioned that I shoudl use functions but I've still not been successful in getting them to work.



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

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 will try and validate any date that you type in.');
writeln(' ');
writeln('The range of years is between 1913 and 2012.');
writeln;
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}
end;
end;

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}

//REPEAT IS NEEDED!!!!!

//BEGIN MENU,INSTRUCTIONS AND USER INPUT
menu;
write('Please enter the date to continue, enter in this format, DD MM YY.');
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;
{ write(monthnames[monthNo]); }
// 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, ' of days in this month!');
writeln('Please select a valid day');
read(dayNo);
until (dayNo <= maxdayNo) and (dayNo >0);
end;
write(monthnames[monthNo]);
dayPrefix;
//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.




Also, I was trying to get the whole thing to repeat but it woudln't. so, instead of it running once, it woudl display the date and then prompt for another. any help here would be good.

thanks.

code_glitch
29-04-2011, 12:52 PM
For your repeat I would add a procedure that sets all values to default/0 and then put your entire main program into a main() procedure. That way between the begin and end of your new program all you will have is



begin
repeat
SetDefaultValues();
Main();
until ApplicationClose = True //or whenever you want the program to end.

[/CODE]

chronozphere
29-04-2011, 10:21 PM
Your code would be cleaner if you could use another function instead of writeln(). One that handles new-line characters. In that case, you can get rid of all the extra writeln() calls for your empty lines. Not sure if such a function exists though.

stevengreen22
29-04-2011, 10:25 PM
Codeglitch - I like the idea, but not too keen on having the whole prog as a proc, is there no other way?

chronozphere, that....is a pretty good point....maybe I can use the ascii char for new line to reproduce it...#13? maybe that'd work?

User137
30-04-2011, 05:54 AM
I don't see where month:string; variable is used? Remember that compiler should give you hints and warnings about any unnormalities in the code, such as this.

If you wanted to make a function version of this make sure to take a backup of your program first. What it would mean is that monthnames[] array would be the only public variable remaining, all others would be moved in to main program. The public variables that you use in procedures would be passed in them as parameters. A procedure that currently changes some public variable would become a function and return the new value for that variable. Then in the main program you would set variable the value that function returns.

Now again, compiler will tell what to do if you encounter errors. We can help describing what they mean if you can't understand them. This is a simple function:

function CombineDate(dd,mm,yy: string): string; // Returns string type variable
begin
result:=dd+'\'+mm+'\'+yy;
end;

// Main program
var dd, mm, yy, fullDate: string;
begin
dd:='30';
mm:='04';
yy:='11';
fullDate := CombineDate(dd,mm,yy);
writeln(fullDate);
// Or more directly without fullDate variable
writeln(CombineDate(dd,mm,yy));
end;