This is really doing my nut in - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

This is really doing my nut in

TimonTimon Posts: 4Member
This is an assignment i was given to make a program in order to print off the Academic year for any given year, taking into account, leap years, starting day e.t.c...

My Problem is this, the program is meant to halt at the readln at the end on the 31st of July, and it does, just not all of the time, i.e. not when the starting day is the 1st or the 7th (possibly other dates too).

This number can be altered easily in the const section against the varible name "startmonday".

Please help me as its now 5 to 6 in the morning and my brain is fried trying to find the error in my program.

Cheers in advance

Timon

[code]
program assign_1; {Written By Timon 2003}
uses crt;
const
startmonday = 6; {Date of the first Monday of the month}
startyear = 2002; {The year the Academic Calendar Starts}
var
weeknumber,monthnumber,date,days,maxdays,endyear,year:integer;
month,day,subtext:string;
begin
days := 1; {Initialization of variables}
date := startmonday;
year := startyear;
endyear := startyear + 1;
weeknumber := 1;
monthnumber := 1;

clrscr; {start of main program}
writeln('Academic Calendar for the year ',startyear,'/',endyear);
writeln('========================================');
writeln;
repeat {Start of Main Loop}

case days of {Naming of Days}
1: day := ('Monday ');
2: day := ('Tuesday ');
3: day := ('Wednesday ');
4: day := ('Thursday ');
5: day := ('Friday ');
end;

case monthnumber of {Naming of Months}
1: month :=('September ');
2: month :=('October ');
3: month :=('November ');
4: month :=('December ');
5: month :=('January ');
6: month :=('Febuary ');
7: month :=('March ');
8: month :=('April ');
9: month :=('May ');
10:month :=('June ');
11:month :=('July ');
end;

case monthnumber of {This section gives the}
1,3,8,10: maxdays := 30; {months their correct lengths}
2,4,5,7,9,11: maxdays := 31;
6: if year mod 4=0 then maxdays :=29 else maxdays := 28;
end;

case date of {This section gives the number of}
1,21,31: subtext := ('st'); {the day its correct prefix}
2,22: subtext := ('nd');
3,23: subtext := ('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: subtext := ('th');
end;

delay (1000); {Adds a delay for slow scrolling of the text}

if monthnumber > 4 then year := endyear; {Checks the year}

write(day,date:2,subtext,' ',month,' ',year); {Writes the main body of}
{text}
if days = 1 then writeln(' Week ',weeknumber) else writeln;
{Writes the week number, but only on a monday}

if days = 5 then {If the day is friday this section will skip two lines}
begin {and increment the week counter as well as the date}
writeln;
writeln;
inc (weeknumber);
date := date + 2;
days := 0; {sets the day to zero so it can be incremented to Monday}
end;

inc (date); {increments the date by 1}

if date > maxdays then {This section checks the date}
{against the maximum days}
begin {allowed in that month and adjusts the date}
inc (monthnumber); {in accordance with the weekend}
if date = (maxdays + 1) then date := 1;
if date = (maxdays + 2) then date := 2;
if date = (maxdays + 3) then date := 3;
end;


inc (days); {increments the day by 1}

until (monthnumber = 12) and (date = 1); {checks to see if the program has}
{reached the end of the calendar}

readln; {Holds the screen for viewing}

end.
[/code]

P.S. This is an assignment for me to do, so if you don't want to tell me the answer to my problem can you at least give me a shove in the right direction?

Cheers again,

Timon.

Comments

  • zibadianzibadian Posts: 6,349Member
    : This is an assignment i was given to make a program in order to print off the Academic year for any given year, taking into account, leap years, starting day e.t.c...
    :
    : My Problem is this, the program is meant to halt at the readln at the end on the 31st of July, and it does, just not all of the time, i.e. not when the starting day is the 1st or the 7th (possibly other dates too).
    :
    : This number can be altered easily in the const section against the varible name "startmonday".
    :
    : Please help me as its now 5 to 6 in the morning and my brain is fried trying to find the error in my program.
    :
    : Cheers in advance
    :
    : Timon
    :
    : [code]
    : program assign_1; {Written By Timon 2003}
    : uses crt;
    : const
    : startmonday = 6; {Date of the first Monday of the month}
    : startyear = 2002; {The year the Academic Calendar Starts}
    : var
    : weeknumber,monthnumber,date,days,maxdays,endyear,year:integer;
    : month,day,subtext:string;
    : begin
    : days := 1; {Initialization of variables}
    : date := startmonday;
    : year := startyear;
    : endyear := startyear + 1;
    : weeknumber := 1;
    : monthnumber := 1;
    :
    : clrscr; {start of main program}
    : writeln('Academic Calendar for the year ',startyear,'/',endyear);
    : writeln('========================================');
    : writeln;
    : repeat {Start of Main Loop}
    :
    : case days of {Naming of Days}
    : 1: day := ('Monday ');
    : 2: day := ('Tuesday ');
    : 3: day := ('Wednesday ');
    : 4: day := ('Thursday ');
    : 5: day := ('Friday ');
    : end;
    :
    : case monthnumber of {Naming of Months}
    : 1: month :=('September ');
    : 2: month :=('October ');
    : 3: month :=('November ');
    : 4: month :=('December ');
    : 5: month :=('January ');
    : 6: month :=('Febuary ');
    : 7: month :=('March ');
    : 8: month :=('April ');
    : 9: month :=('May ');
    : 10:month :=('June ');
    : 11:month :=('July ');
    : end;
    :
    : case monthnumber of {This section gives the}
    : 1,3,8,10: maxdays := 30; {months their correct lengths}
    : 2,4,5,7,9,11: maxdays := 31;
    : 6: if year mod 4=0 then maxdays :=29 else maxdays := 28;
    : end;
    :
    : case date of {This section gives the number of}
    : 1,21,31: subtext := ('st'); {the day its correct prefix}
    : 2,22: subtext := ('nd');
    : 3,23: subtext := ('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: subtext := ('th');
    : end;
    :
    : delay (1000); {Adds a delay for slow scrolling of the text}
    :
    : if monthnumber > 4 then year := endyear; {Checks the year}
    :
    : write(day,date:2,subtext,' ',month,' ',year); {Writes the main body of}
    : {text}
    : if days = 1 then writeln(' Week ',weeknumber) else writeln;
    : {Writes the week number, but only on a monday}
    :
    : if days = 5 then {If the day is friday this section will skip two lines}
    : begin {and increment the week counter as well as the date}
    : writeln;
    : writeln;
    : inc (weeknumber);
    : date := date + 2;
    : days := 0; {sets the day to zero so it can be incremented to Monday}
    : end;
    :
    : inc (date); {increments the date by 1}
    :
    : if date > maxdays then {This section checks the date}
    : {against the maximum days}
    : begin {allowed in that month and adjusts the date}
    : inc (monthnumber); {in accordance with the weekend}
    : if date = (maxdays + 1) then date := 1;
    : if date = (maxdays + 2) then date := 2;
    : if date = (maxdays + 3) then date := 3;
    : end;
    :
    :
    : inc (days); {increments the day by 1}
    :
    : until (monthnumber = 12) and (date = 1); {checks to see if the program has}
    : {reached the end of the calendar}
    :
    : readln; {Holds the screen for viewing}
    :
    : end.
    : [/code]
    :
    : P.S. This is an assignment for me to do, so if you don't want to tell me the answer to my problem can you at least give me a shove in the right direction?
    :
    : Cheers again,
    :
    : Timon.
    :
    The main problem is that you sometimes increase the date by more than 1 during a loop. This when this occurs at the end of juli, then you might miss the 1st of august. The solution to this is quite simple: remove the (date = 1) from the until. Now your program will run until it reaches the entire month of august, so not just august 1st.
  • ManningManning Posts: 1,621Member
    : : This is an assignment i was given to make a program in order to print off the Academic year for any given year, taking into account, leap years, starting day e.t.c...
    : :
    : : My Problem is this, the program is meant to halt at the readln at the end on the 31st of July, and it does, just not all of the time, i.e. not when the starting day is the 1st or the 7th (possibly other dates too).
    : :
    : : This number can be altered easily in the const section against the varible name "startmonday".
    : :
    : : Please help me as its now 5 to 6 in the morning and my brain is fried trying to find the error in my program.
    : :
    : : Cheers in advance
    : :
    : : Timon
    : :
    : : [code]
    : : program assign_1; {Written By Timon 2003}
    : : uses crt;
    : : const
    : : startmonday = 6; {Date of the first Monday of the month}
    : : startyear = 2002; {The year the Academic Calendar Starts}
    : : var
    : : weeknumber,monthnumber,date,days,maxdays,endyear,year:integer;
    : : month,day,subtext:string;
    : : begin
    : : days := 1; {Initialization of variables}
    : : date := startmonday;
    : : year := startyear;
    : : endyear := startyear + 1;
    : : weeknumber := 1;
    : : monthnumber := 1;
    : :
    : : clrscr; {start of main program}
    : : writeln('Academic Calendar for the year ',startyear,'/',endyear);
    : : writeln('========================================');
    : : writeln;
    : : repeat {Start of Main Loop}
    : :
    : : case days of {Naming of Days}
    : : 1: day := ('Monday ');
    : : 2: day := ('Tuesday ');
    : : 3: day := ('Wednesday ');
    : : 4: day := ('Thursday ');
    : : 5: day := ('Friday ');
    : : end;
    : :
    : : case monthnumber of {Naming of Months}
    : : 1: month :=('September ');
    : : 2: month :=('October ');
    : : 3: month :=('November ');
    : : 4: month :=('December ');
    : : 5: month :=('January ');
    : : 6: month :=('Febuary ');
    : : 7: month :=('March ');
    : : 8: month :=('April ');
    : : 9: month :=('May ');
    : : 10:month :=('June ');
    : : 11:month :=('July ');
    : : end;
    : :
    : : case monthnumber of {This section gives the}
    : : 1,3,8,10: maxdays := 30; {months their correct lengths}
    : : 2,4,5,7,9,11: maxdays := 31;
    : : 6: if year mod 4=0 then maxdays :=29 else maxdays := 28;
    : : end;
    : :
    : : case date of {This section gives the number of}
    : : 1,21,31: subtext := ('st'); {the day its correct prefix}
    : : 2,22: subtext := ('nd');
    : : 3,23: subtext := ('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: subtext := ('th');
    : : end;
    : :
    : : delay (1000); {Adds a delay for slow scrolling of the text}
    : :
    : : if monthnumber > 4 then year := endyear; {Checks the year}
    : :
    : : write(day,date:2,subtext,' ',month,' ',year); {Writes the main body of}
    : : {text}
    : : if days = 1 then writeln(' Week ',weeknumber) else writeln;
    : : {Writes the week number, but only on a monday}
    : :
    : : if days = 5 then {If the day is friday this section will skip two lines}
    : : begin {and increment the week counter as well as the date}
    : : writeln;
    : : writeln;
    : : inc (weeknumber);
    : : date := date + 2;
    : : days := 0; {sets the day to zero so it can be incremented to Monday}
    : : end;
    : :
    : : inc (date); {increments the date by 1}
    : :
    : : if date > maxdays then {This section checks the date}
    : : {against the maximum days}
    : : begin {allowed in that month and adjusts the date}
    : : inc (monthnumber); {in accordance with the weekend}
    : : if date = (maxdays + 1) then date := 1;
    : : if date = (maxdays + 2) then date := 2;
    : : if date = (maxdays + 3) then date := 3;
    : : end;
    : :
    : :
    : : inc (days); {increments the day by 1}
    : :
    : : until (monthnumber = 12) and (date = 1); {checks to see if the program has}
    : : {reached the end of the calendar}
    : :
    : : readln; {Holds the screen for viewing}
    : :
    : : end.
    : : [/code]
    : :
    : : P.S. This is an assignment for me to do, so if you don't want to tell me the answer to my problem can you at least give me a shove in the right direction?
    : :
    : : Cheers again,
    : :
    : : Timon.
    : :
    : The main problem is that you sometimes increase the date by more than 1 during a loop. This when this occurs at the end of juli, then you might miss the 1st of august. The solution to this is quite simple: remove the (date = 1) from the until. Now your program will run until it reaches the entire month of august, so not just august 1st.
    :

    Oops didn't notice you replied already. I'll kill my post since it said the exact same thing.
Sign In or Register to comment.