Member Avatar for DigitalPackrat

I have been assigned to write a program to print the calendar of a given year. Its almost complete, but, I haven't yet found a proper algorithm to find the first day of the year which is required by the function to print the calendar year.

Will provide the code if necessary.

The easiest way is to use the time library:

#include <ctime>
#include <iostream>

int main()
{
    std::time_t temp = std::time ( 0 );
    std::tm *first = std::localtime ( &temp );

    first->tm_year = 2008 - 1900;
    first->tm_mday = 1;
    first->tm_mon = 0;

    if ( std::mktime ( first ) != (std::time_t)-1 ) {
        char weekday[100];

        if ( std::strftime ( weekday, 100, "%A", first ) > 0 )
            std::cout<<"The first day of the year is "<< weekday <<'\n';
    }
}
Member Avatar for DigitalPackrat

Thanks, but, I was looking for a more simple solution in terms of formula or such.
I found a formula but, it does not seem to be working.

Since we can't see either the formula you've chosen, or how you've implemented it, there isn't much we can do.

Post your code.

In your link, scroll down to "How do I find the day of the week for any date?"
Calculate the result for 1st January for your input year.

Member Avatar for DigitalPackrat

In your link, scroll down to "How do I find the day of the week for any date?"
Calculate the result for 1st January for your input year.

edit: The link was this http://mathforum.org/library/drmath/view/55837.html
This one seems a lot easier, but, is not working.

>I was looking for a more simple solution in terms of formula or such.
An ad hoc solution isn't going to be simpler. It's also going to be less powerful, less flexible, less accurate, and potentially buggy. But you go ahead and use your "simpler" solution and I won't bother offering a better one next time. :icon_rolleyes:

>Thanks anyway.
"Thanks anyway" is a polite sounding way of saying "You were no help, I solved my own problem, so long losers". I'd recommend avoiding that particular phrase in favor of "Thanks for your help".

Member Avatar for DigitalPackrat

>I was looking for a more simple solution in terms of formula or such.
An ad hoc solution isn't going to be simpler. It's also going to be less powerful, less flexible, less accurate, and potentially buggy. But you go ahead and use your "simpler" solution and I won't bother offering a better one next time. :icon_rolleyes:

>Thanks anyway.
"Thanks anyway" is a polite sounding way of saying "You were no help, I solved my own problem, so long losers". I'd recommend avoiding that particular phrase in favor of "Thanks for your help".

Alright, let me put this in this way - I am still starting out with C++, so, I found your solution a bit complex and more importantly I was looking for a mathematical formula. A mathematical formula when proven right can in no way be called "ad hoc". "Less powerful" - maybe; "Less flexible" - agreed; "Less accurate" - no way; "Potentially buggy" - read my last line.

I might sound arrogant here but, I really had to answer this, this way.
And thanks for the help.

>A mathematical formula when proven right can in no way be called "ad hoc".
Sure it can. It's like rewriting bubble sort each time you need it. Of course, that formula also hasn't been proven right as far as I can tell. There's no formal proof that I could find in your link, it just looks like some random dude got bored and spent an hour coming up with it.

>"Less powerful" - maybe
You mean definitely. Your mathematical formula does one thing, and only one thing. To do something else, you need a completely different formula. That's clearly less powerful.

>"Less accurate" - no way
Really. It seems you haven't done much date programming, because it's a lot more complicated than you seem to think. The only way you can turn a general date question into a clean formula is to make assumptions and significantly limit the date range and/or accuracy of the result. If you restrict yourself to those limitations the formula will be accurate, but with the limitations in place, you can't say that your formula answers the general question.

>"Potentially buggy" - read my last line.
You're right. Change that to "definitely buggy", because anyone with your attitude about solutions clearly doesn't have correctness in mind.

#include<iostream>
using namespace std;

void clear(void);

int main()
{
    char ch;
    int year,mon,shy;
    int ivoid, maxday;

    int day[31]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
        16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

    char month[12][10]=
        {"January","February","March","April","May","June",
        "July","August","September","October","November","December"};

    char weekday[7][10]={"Monday","Tuesday","Wednesday","Thursday",
        "Friday","Saturay","Sunday"};

    year=1984;
    mon=7;
    const int cyear=2008;
    const int cnon=1;
    ivoid=1;
    do{
    cout<<"Enter year: ";
    cin>>year;
    clear();
    cout<<"Enter number of month: ";
    cin>>mon;
    clear();

    if((year-cyear)==0)
        shy=0;
    else if((year-cyear)>0)
    {
        shy=(year-cyear)+int((year-cyear)/4);
        if ((mon==1||mon==2)&&year%4!=0) shy++;
    }
    else if((year-cyear)<0)
    {
        shy=(year-cyear)-int((cyear-year)/4);
        if ((mon!=1 && mon!=2)&&year%4!=0) shy--;
    }

    if     (mon==1)  ivoid=shy+1;
    else if(mon==2)  ivoid=shy+4;
    else if(mon==3)  ivoid=shy+5;
    else if(mon==4)  ivoid=shy+1;
    else if(mon==5)  ivoid=shy+3;
    else if(mon==6)  ivoid=shy+6;
    else if(mon==7)  ivoid=shy+1;
    else if(mon==8)  ivoid=shy+4;
    else if(mon==9)  ivoid=shy+0;
    else if(mon==10) ivoid=shy+2;
    else if(mon==11) ivoid=shy+5;
    else if(mon==12) ivoid=shy+0;

    while(ivoid<0)
        ivoid=ivoid+7;
    ivoid=ivoid%7;

    if (mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
        maxday=31;
    else if(mon==4||mon==6||mon==9||mon==11)
        maxday=30;
    else if(mon==2)
    if(year%4==0)
        maxday=29;
    else
        maxday=28;


    cout<<" .................. "<<month[mon-1]<<", "<<year<<" ...................\n";
    cout<<"....................................................\n";
    cout<<"Monday Tuesday Wednes Thursday Friday Saturay Sunday\n";


    for (int v=0;v<ivoid; v++)
        cout<<"\t";

    for(int i=0; i<maxday;i++)
    {
        cout<<day[i]<<"\t";
        if((ivoid+i+1)%7==0)
            cout<<"\n\n";
    }

    cout<<"\n";

    cout<<"Enter any kay for continue.   < Q > - quite\n";
    cin>>ch;

    }
    while(ch!='q'&& ch!='Q');

    return 0;
}



void clear(void)
{
    if(!cin)
    {
        cin.clear();
        while (cin.get()!='\n')
        continue;
        cout<<"Bad input!\a\n";
    }
}
commented: Don't bump old threads -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.