day from date calculator

Discussion in 'Web Design & Programming' started by zeus, Nov 18, 2006.

  1. zeus

    zeus out of date

    Likes Received:
    0
    Trophy Points:
    36
    Well ive finished my first c++ program. Its a program which converts a date into a weekday name.

    It took all day and im sure ive done things the hard way but im happy with it.

    Heres the program...
    day from date calculator

    There are a few things I want to change but I dont think I know the code id need to use. Ive only done the basics of c++ section on C++ Language Tutorial

    I want it to ask for a solid date, ie 1289, instead of 12 then 89. I cant get my program to split the first two digits from the second two. Again, its just because i haven't read far enough!

    Here is the code
    Code:
    //This program will find the day from any date between 45BC to 9999AD.
    //It uses the Julian Calendar between the dates 45BC and 1752AD. 
    //It assumes 45BC (the year the Julian calendar was introduced) is a leap year.
    //The year does not exist, 3BC, 2BC, 1BC, 1AD....
    //Type 'config' to change the date the Gregorian calendar takes effect.
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    //Collect Data
    int england=1752;
    start:
    cout << "\nDDDDD           tt";                                     
    cout << "\nDD  DD    aa aa tt      eee";
    cout << "\nDD   DD  aa aaa tttt  ee   e";  
    cout << "\nDD   DD aa  aaa tt    eeeee";       
    cout << "\nDDDDDD   aaa aa  tttt  eeeee\n";   
        
    cout << "\n CCCCC          lll                lll         tt";
    cout << "\nCC    C   aa aa lll   cccc uu   uu lll   aa aa tt     oooo  rr rr";
    cout << "\nCC       aa aaa lll cc     uu   uu lll  aa aaa tttt  oo  oo rrr  r";   
    cout << "\nCC    C aa  aaa lll cc     uu   uu lll aa  aaa tt    oo  oo rr";
    cout << "\n CCCCC   aaa aa lll  ccccc  uuuu u lll  aaa aa  tttt  oooo  rr";
    cout << "\n\nby Rik Potts";   
        
        string start;
        cout << "\n\nEnter 'go' to begin or type 'config' for configuration: ";
        cin >> start;
        
        if (start!="go" && start!="config") 
    {
        cout << start << " is not a valid entry. Please try again\n\n";
        goto start;
    }
        
        if (start=="go") goto findday; else goto config;
        
        //Configuration     
        config:    
        cout << "\nPlease enter the date you want the Gregorian (modern) calendar to take effect.";
        cout << "\n\nDefault is 1752 which is when England began using the Gregorian";
        cout << "\ncalendar. The Gregorian calendar was first introduced in 1582";
        cout << "\nin Venice, France, Spain, Portugal and Netherlands. Germany in 1583";
        cout << ",\nPrussia and Denmark in 1700, Sweden in 1753 and Russia in 1918!\n\n";
        cout << "Please enter the date you want the Gregorian calendar to take effect: ";
        cin >> england;
        cout << "NOTE: You will have to set config each time you run the program.\n\n";
        
        //Find the day (day)   
    findday:
        cout << "The Gregorian (modern) calendar will take effect from " << england;
        
        int day;
        cout << "\nPlease enter the day (##): " ;
        cin >> day;
        
        if (day>31 || day<1)
    {   
        cout << "\n" << day << " is not a valid day!\nPlease try again\n\n"; 
        goto findday;
    }
        
        //Find the month (month)...
    month:
        string month;
        cout << "Please enter the Month (abc): ";
        cin >> month;
        
        if (month!="feb" && month!="aug" && month!="mar" && month!="nov" 
        && month!="jun" && month!="sep" && month!="dec" && month!="apr" 
        && month!="jul" && month!="jan" && month!="oct" && month!="may")
    {   
        cout << "\n" << month << " is not a valid month!\nPlease try again...\n\n";
        goto month;
    }
        
        if (month=="feb" && day>29) 
    {
        cout << "\nIt is not possible to have " << day << " days in February!";
        cout << "\nPlease try again\n\n";
        goto findday;
    }
    
         //Find the year (date)...
    cent:
        cout << "\nNOTE: If you need a BC date you DO ";
        cout << "NOT need to use a minus sign (-)\n";
        
        //Get the century (cent)
        int cent;
        cout << "Please enter the first two digits of the year (##__): ";
        cin >> cent;
        
        if (cent>99) 
    {
        cout << "\n" << cent << " is not a valid entry.\nYou must enter a number below 99.\n";
        goto cent;
    }
        
        //Get the year (year)
    year:
        int year;
        cout << "Please enter the last two digits of the year (__##): ";
        cin >> year;
        
        if (year>99 || year<0) 
    {
        cout << "\n" << year << " is not a valid entry.\nYou must enter a number below 99\n\n";
        goto year;
    }
    
        //make the full year (date)
        int date, numdate;
        date=(100*cent) + year;
        numdate=date;
        
        if (date==0)
    {
        cout << "\nThe date 0000 is not valid because the Julian calendar uses 1BC";
        cout << " instead\n\n";
        goto cent;
    }
    
        //Is it a negative or positive year?...
    norp:
        string bc;
        cout << "Is the year BC? (y/n) : ";
        cin >> bc;
        
        if (bc!="y" && bc!="n") 
    {
        cout << bc << " is not a valid option. Please try again...";
        goto norp;
    }
        
        if (bc=="y") date=-date;
        
        if (date<=-46) 
    {
        cout << "\nYou entered " << numdate << "BC. The Julian calendar was introduced ";
        cout << "in 45BC.";
        cout << "\nThis makes " << numdate << "BC invalid!\n";
        goto cent;
    }
        
        int year2;
        year2=year/4;
        
    //Process and Validate Data
        //Is the year a leap year?...
        //Is the year on the gregorian or julian calendar?
        int yl=1, nl=0;
        if (date>=england) goto gregorian;
        
        //Julian calendar
        int jleap;  
        if (date>0 && (date%4==0)) jleap=yl; else jleap=nl;
        if (date<0 && (date%4==-1)) jleap=yl; else jleap=nl;
        goto validation;
       
        //Gregorian calendar
    gregorian:
        int gleap;
        gleap = ((date%4==0 && date%100!=0) || date % 400 == 0) ? yl : nl;
    
        //Is the Month and Day combination correct?
    validation:
        if ((jleap==1 || gleap==1) && month=="feb" && day<=29) goto notfeb;
        
        if ((jleap!=1 || gleap!=1) && month=="feb" && day==29) 
    {
        cout << "\nThe 29th February is not a valid date because " << date;
        cout << " \nisnt a leap year!\nPlease try again from the beginning\n\n"; 
        goto findday;
    }
        
        if (month!="feb") goto notfeb;
        
        if (!(day<=28 && day>=1)) 
    {
        cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
        goto findday;
    }
    
    notfeb:
        if (month!="sep" && month!="apr" && month!="jun" && month!="nov") goto nsajn;
        
        if (!(day<=30 && day>=1)) 
    {
        cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
        goto findday;
    }
    
    nsajn:
        if (!(day<=31 && day>=1))
    {
        cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
        goto findday;
    }
    
    //Interpret Data
        //Calculate the Monthcode
        int monthcode;
        if (month=="aug") monthcode=0;
        if (month=="feb" || month=="mar" || month=="nov") monthcode=1;
        if (month=="jun") monthcode=2;
        if (month=="sep" || month=="dec") monthcode=3;
        if (month=="apr" || month=="jul") monthcode=4;
        if (month=="jan" || month=="oct") monthcode=5;
        if (month=="may") monthcode=6;
        
        //Which calendar to use?
        if (date>=england) goto gregoriansum;
    
        //Is the leap year compensation required?
        int ujleap, ugleap;
        ujleap=jleap;
        ugleap=gleap;
        if (month!="jan" && month!="feb") ujleap=nl;
        if (month!="jan" && month!="feb") ugleap=nl;
        
        //Julian Formula...
        //(Day + MonthCode + Year + Year/4 + 5 – Century – isLeapJanFeb?) % 7
        int jresult;
        if (date<0) jresult=(day + monthcode - year - (year2) + 5 - cent )%(-7);
        else
        jresult=(day + monthcode + year + (year2) + 5 - cent - ujleap)%7;
        
        goto weekday;
        
        //Gregorian Formula...
    gregoriansum:
        //(Day + MonthCode + Year + Year/4 – 2×(Century%4) – isLeapJanFeb?) % 7
        int gresult;
        gresult=(day + monthcode + year + (year2) - (2*(cent%4)) - ugleap)%7;
        
        //Assign day code to word.
    weekday:
        int weekday;
        string result;
        weekday = (date<england) ? jresult : gresult;
        if (weekday==0) result="Monday";
        if (weekday==1 || weekday==-6) result="Tuesday";
        if (weekday==2 || weekday==-5) result="Wednesday";
        if (weekday==3 || weekday==-4) result="Thursday";
        if (weekday==4 || weekday==-3) result="Friday";
        if (weekday==5 || weekday==-2) result="Saturday";
        if (weekday==6 || weekday==-1) result="Sunday";
        
    //Print results
        if (date<0)
        cout << "\nThe day on " << day << " " << month << " " << numdate << "BC is a " 
        << result << "!\n";
    
        else 
        cout << "\nThe day on " << day << " " << month << " " << date << "AD is a " 
        << result << "!\n";
    
        if (date==england) 
    {
        cout << "\nYou entered the year 1752!";
        cout << "\nThis was the year the Gregorian calendar was first ";
        cout << "\nproposed by Dr Aloysius Lilius!\n";
    }
        
        if (date==(-45)) 
    {
        cout << "\nYou entered the year 45BC!";
        cout << "\nThis was the year the Julian calendar was first ";
        cout << "\nintroduced by Julius Caesar!\n";
    }
        
        if (date<0 && (jleap==1))
    {
        cout << "The year " << numdate << "BC is a leap year!" << "\n\n";
        goto rerun;
    }
        
        if (date>0 && (jleap==1 || gleap==1))
    {   
        cout << "The year " << date << "AD is a leap year!" << "\n\n";
        goto rerun;
    }
        
        if (date<0 && (jleap!=1))
    {   
        cout << "The year " << numdate << "BC isnt a leap year!" << "\n\n";
        goto rerun;
    }
        
        if (date>0 && (jleap==0 || gleap==0))
        cout << "The year " << date << "AD isnt a leap year!" << "\n\n"; 
        goto rerun;
    
    rerun:
        cout << "Do you want to run the program again? (y/n): ";
        string startagain;
        cin >> startagain;
        cout << "\n";
        if (startagain=="y") goto findday; else goto close;
        
        close:
        return 0;
    }
    
     
  2. Karanislove

    Karanislove It's D Grav80 Of Luv

    Likes Received:
    0
    Trophy Points:
    36
    nice lill program! lol...i was born on Sunday...neva bothered to knw this before!
     
  3. zeus

    zeus out of date

    Likes Received:
    0
    Trophy Points:
    36
    Ive started this again.
    My heads frying. Different dates use different calendars and different calendars use different leap year rulres. Year 0000 is really year 1BC, no-one on earth can give a definitive answer to whether 45BC is a leap year or not and for some reason my calculator is multiply the date by 10!

    I cant find a single calculator which does everything correctly so I can check mine against it. Most use the gregorian calendar before 1582, most say 0000 is a date too. The ones that say 0000 isnt a date say 1BC isnt a leap year... Its a pain in the arse.

    It has made me realise how so many people do a half arse job on stuff.

    The only day calculator on the whole of the net which id put trust in is this one.... And mine when it stops saying the date is 16660 for 1666 etc!
    Calendar Converter
    All the other ones which come up in google are incomplete and accept dates which are beyond the scope of the formula the calculator uses.
     
  4. daisycutter

    daisycutter Geek

    Likes Received:
    0
    Trophy Points:
    6
    fun little distraction! at least i know now that my birthday falls on a saturday in 3577!:chk:
     
  5. zeus

    zeus out of date

    Likes Received:
    0
    Trophy Points:
    36
    I think ive finally cracked it!
    As far as I know its only the second program easily found on Google (actually this probably isnt easily found!) which uses the correct formula for the correct date!

    The link I posted above is the only other place. Though have a look at the year 24BC. There is a one day discrepancy. Ive checked it by hand and im sure mine is correct... not unless there is a third formula for dates including 24BC. Ive found no reference to a third one though.

    I want to add a few bits here and there.... ie "On this day......" etc
    Anyway, on with the turorial.
     
  6. Willz

    Willz MiCrO$oFt $uK$ :D

    Likes Received:
    36
    Trophy Points:
    48
    Nice program :good:, i wish at college i learned C++ than vB, i mean, i could create programs for my keyboard when it comes :p, and C++ seems more intresting than vB.
     

Share This Page