Too old to signed int

The following program could not compute current days e.g. May 15, 2017. But it could compute my birth date. The reason was the Integer type in line 2.

program dateOfBirth;
var daysbetween: Integer;
var birthyear,birthmonth,birthday: Integer;
var monthdays: array[1..12] of Integer;
var yearcounter,monthcounter: Integer;
var weekday: Integer;

begin
  monthdays[1] := 31;
  monthdays[2] := 28;
  monthdays[3] := 31;
  monthdays[4] := 30;
  monthdays[5] := 31;
  monthdays[6] := 30;
  monthdays[7] := 31;
  monthdays[8] := 31;
  monthdays[9] := 30;
  monthdays[10] := 31;
  monthdays[11] := 30;
  monthdays[12] := 31;
  
  Write('Please give year of birth:');
  Read(birthyear);
  Write('Please give month of birth:');
  Read(birthmonth);
  Write('Please give day of birth:');
  Read(birthday);

daysbetween := 0; (*1st day of 1900*)
(*previous years*)
for yearcounter:=1900 to birthyear-1 do
begin
  if (yearcounter mod 400 = 0) then
    daysbetween := daysbetween + 366
  else
    if ((yearcounter mod 100 <> 0) and (yearcounter mod 4 = 0)) then
      daysbetween := daysbetween + 366
    else
      daysbetween := daysbetween + 365
end;

(*year of birth*)
for monthcounter := 1 to birthmonth-1 do
  daysbetween := daysbetween +
  monthdays[monthcounter];
  if ((birthyear mod 400 = 0) or ((birthyear mod 100 <> 0) and (birthyear mod 4 = 0))) then
  if (birthmonth > 2) then 
    daysbetween := daysbetween + 1;

daysbetween := daysbetween + birthday;
weekday := daysbetween mod 7;

case weekday of
  0: writeln('Sunday');
  1: writeln('Monday');
  2: writeln('Tuesday');
  3: writeln('Wednesday');
  4: writeln('Thursday');
  5: writeln('Friday');
  6: writeln('Saturday');
end;
end.

I changed that to Longint and it did the trick.

var daysBetween: Longint;

So, if you are old enough, born before September 30, 1989 an integer can hold the number of days between your date of birth and 1/1/1900.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s