PDA

View Full Version : Some problems with booleans....


RedLion
January 17th, 2007, 20:49
Ok, I'm currently doing my homeworks for our programming teacher(:yawn: ) and I'm working on this old good Pascal(beurk). But I got a problem. Here we go...
Here's a part of my code:

program Date_and_day;
var day,month,year:byte;
kmfevbis,kmfevnbis,kmopair,kmoimp,valdate,ansnonva l:boolean;
answer:char;
begin

(*A loop for going on*)
Repeat

(*Ask user the date*)
writeln('Give the day please');
readln(day);
writeln('Give the month please');
readln(month);
writeln('Give the year please');
readln(year);

(*Initialiazing booleans for the conditions of a good date*)
kmfevbis:=(month=2)and (year mod 4 <> 0)and (1<=day) and (day<=28);
kmfevnbis:=(month=2) and (year mod 4 = 0) and (1<=day) and (day<=29);
kmopair:=(((month=4) or (month=6) or (month=9) or (month=11)) and ((1<=day) and (day<=30)));
kmoimp:=(((month=1) or (month=3) or (month=5) or (month=7) or (month=8) or (month=10) or (month=12)) and ((1<=day) and (day<=31)));
valdate:=(kmfevbis or kmfevnbis or kmopair or kmoimp);

(*Check the validity of the date*)
If not valdate then

repeat writeln('Give a correct date please');

writeln('Give the day please');
readln(day);
writeln('Give the month please');
readln(month);
writeln('Give the year please');
readln(year);

until valdate;

else writeln('The date is correct');
end
So the main issue is when I gave a bad date even if I give a good date after I don't go out of loop "repeat" and it keep asking me to enter a correct date; I can do it old school but i really want to use booleans coz they really clarify the code. Can someone tell me what I'm doing wrong please? many thanks for your help!

darth sephiroth
January 17th, 2007, 21:15
maybe you have to repeat the validation. I can't see the script pointing it back to get re-checked after it is not validated. (i'm a newb a script, but i think you're telling it to vait for a validation, without doing anything to get that validation)

and alternative is to do as many do, supply a drop-down box with all the valid dates.

RedLion
January 18th, 2007, 11:31
You were almost right. In fact I made a beginner mistake. I had a very bad management of the loop.The booleans must be in the loop not outside. I've solved it.Thanks darth sephiroth for your answer.