fdfgsdfgsdfg
fdfgsdfgsdfg

Reputation: 37

Leap years not working in Date and Time program in Dlang

I haven't been programming very long, so I'm practicing some logic exercises in dlang. Any ideas on what I've done wrong here. When I get to a leap year, my program just keeps looping on the WHILE.

import std.stdio;
void main()
{
bool dead;
string thing;
int phew = 5; //days
int tahr = 1; //months
int tron; //monthsDate
string[7] days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"];
int date = 28;
string[12] months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
int year = 1996;
int hours = 11;
int mins = 28;
string ampm = "pm";
bool ly;
int leap = 1996;
int cen = 996;
//writeln("This program is incomplete. Obviously.");
write("Press Enter to Continue.");
readf("%s\n",&thing);
while(!dead)
{
    while(hours <= 12)
    {
        while(mins <= 59)
        {
            if(mins < 10)
                write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":0",mins,ampm,": ");
            else
                write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":",mins,ampm,": ");
            readf("%s\n",&thing);
            mins++;
        }
        hours++;
        if(hours == 12 && ampm == "am")
        {
            ampm = "pm";
        }
        else if (hours == 12 && ampm == "pm")
        {
            ampm = "am";
            phew++;
            date++;
            if(phew > 6)
                phew = 0;
            if((date == 29 || date == 30) && tahr == 1)
            {
                while(leap <= year) //this assuming time travel doesn't work
                { //reminder: add time travel
                    if (leap == year)
                    {
                        ly = true;
                        break;
                    }
                    leap+=4;
                    ly = false;
                }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap-=4;
                }
            }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap-=4;
                }
            }
            if(date == 31 && (tahr == 1 || tahr == 3 || tahr == 5 || tahr == 8 || tahr == 10))
            {
                date = 1;
                tahr++;
            }
            else if (tahr == 11 && date == 32)
            {
                tahr = 0;
                date = 1;
                year++;
                cen++;
                if(cen == 1000)
                {
                    writeln("Happy Millennium!");
                    cen = 0;
                }
                else
                    writeln("Happy New Year!");
            }
            else if(date == 32 && (tahr == 0 || tahr == 2 || tahr == 4 || tahr == 6 || tahr == 7 || tahr == 9))
            {
                date = 1;
                tahr++;
            }
        }

        if(hours == 13)
        {
            hours = 1;
        }
        mins = 0;
    }
}
}

The important part is this:

if((date == 29 || date == 30) && tahr == 1)
            {
                while(leap <= year) //this assuming time travel doesn't work
                { //reminder: add time travel
                    if (leap == year)///
                    {
                        ly = true;
                        break;
                    }
                    leap+=4;
                    ly = false;
                }
                if(!ly || date == 30)
                {
                    date = 31;
                    leap=-4;
                }
            }

So, I figured out the issue almost immediately after posting. Basically, I wrote =+ instead of +=. Very simple mistake. That's what I get for typing too fast. So, I've fixed up the code now, if you have any other suggestions, make sure to put them in the comments. Thanks.

Upvotes: 0

Views: 322

Answers (2)

Colin Grogan
Colin Grogan

Reputation: 2832

I haven't read it in depth, but I recommend you add lots of debug writelns. Particularly just inside the block you're THINKING should get run:

if((date == 29 || date == 30) && tahr == 1) { 
    writefln("Handling leap years...");
    while(leap <= year) //this assuming time travel doesn't work
    { //reminder: add time travel
        writefln("In loop, Leap <= year? %s", leap <= year);

This will allow you to debug more what is actually happening when your program is run.

Upvotes: 0

fdfgsdfgsdfg
fdfgsdfgsdfg

Reputation: 37

Decided to print out leap into the terminal, and I figured out my issue. Leap was continuously equal to 4, because I wrote =+ instead of +=. It was just a case of clicking the wrong button first. That's what I get for typing fast. The program works now, as far as I know. Feel free to correct me on anything else you may notice.

Upvotes: 2

Related Questions