PDA

View Full Version : repeat needed



stevengreen22
04-05-2011, 09:28 PM
Hi guys,

Overall i'm happy with this program as it is, in the write up i've got a fair few examples of how to improve it using functions etc but...what i really want, is a working repeat for th ewhole thing.

can anyone help? i've tried using repeat until and so on but (when you lok at the code) you'll see the issue i have. the input is read as day, month adn year, they're read as integers so i can't have 'press 0 to quit' etc and i'm not knowledgeable enough to use 'x to quit'

any help please?



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.

User137
05-05-2011, 07:04 AM
I might suggest another readln() at the end of program that asks "Test again? (y/n): ", and read it in char variable. Then use that in repeat-until that goes from beginning to end.

You are right about that if you have readln(dayNo,monthNo,yearNo); it cannot be used for reading "x". It would have to be readln(stringVar) and then 3 numbers or the "x" parsed from it. But that goes harder than your example needs.

ps. Third thread about same topic?

stevengreen22
05-05-2011, 07:49 AM
Thankyou, I was so wrapped up in trying to get one wya to work that I didn't look at other options. I've also had barely any dealings with using char within the programs, it was touched on in the ascsii program butnothing since so i'm a little anxious about getting error messages and so on when the prog seems to be working.

I added a 'yesNo' readln as you suggested with 'y' and 'n' being the appropriate answers. I think I'm missing something stupid or I might need to decalre them as true / false first as the program now continually repeats and doesn't distinguish between y/n or any other letter for that matter. The condition is repeat....blaaaaaa.....until yesNo = N; can you help any further? is it the true & false initialising that I need?

3rd thread...lesson learnt....:D

stevengreen22
05-05-2011, 10:14 AM
SOlved it, was loking at it wrong and making it more difficult than need be, Used your suggestion but changed it so 0 would exit and any other number would continue.

Not as pretty but it works and it needs to be handset in tomorrow, happy days! thanks for the suggestion.