1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ code, the calender

  1. Feb 8, 2010 #1
    Hello...
    the is my code:
    #include<iostream.h>
    #include<iomanip.h>

    void show(int m, int w, int n)
    {
    cout<<endl<<"Sun Mon Tue Wed Thu Fri Sat\n";
    cout<<setw((w*5)+1);
    for(int r=1;r<=(7-w);r++) cout<<r<<" ";
    int count=0;
    cout<<endl;
    for (;r<=n;r++)
    {
    if(r<10)
    {
    cout<<r<<" ";
    count++;
    }
    else
    {
    cout<<r<<" ";
    count++;
    }
    if((count%7)==0)
    cout<<endl;
    }
    }
    void print(int v, int q,int y)
    {
    int nd;
    switch (v)
    {
    case 1 :
    nd=31;
    cout<<"\n\n\nJan\n";
    show(v,q,nd);
    break;

    case 2:
    if(y%4==0)
    nd=29;
    else
    nd=28;
    cout<<"\n\n\nFeb\n";
    show(v,q,nd);
    break;

    case 3 :
    nd=31;
    cout<<"\n\n\nMar\n";
    show(v,q,nd);
    break;

    case 4:
    nd=30;
    cout<<"\n\n\nApr\n";
    show(v,q,nd);
    break;

    case 5 :
    nd=31;
    cout<<"\n\n\nMay\n"; show(v,q,nd);
    break;

    case 6:
    nd=30;
    cout<<"\n\n\nJun\n";
    show(v,q,nd);
    break;

    case 7 :
    nd=31;
    cout<<"\n\n\nJul\n";
    show(v,q,nd);
    break;

    case 8 :
    nd=31;
    cout<<"\n\n\nAug\n";
    show(v,q,nd);
    break;

    case 9 :
    nd=30;
    cout<<"\n\n\nSep\n";
    show(v,q,nd);
    break;

    case 10 :
    nd=31;
    cout<<"\n\n\nOct\n";
    show(v,q,nd);
    break;

    case 11:
    nd=30;
    cout<<"\n\n\nNov\n";
    show(v,q,nd);
    break;

    case 12:
    nd=31;
    cout<<"\n\n\nDec\n";
    show(v,q,nd);
    break;
    }

    }

    int main()
    { int x;
    int year;
    cout<<" Welcome \n *******"<<endl<<"enter the year";
    cin>>year;
    int a= (int)(year/100);
    int b= a*100 ;
    int c= (year-b);
    int d;
    if((b>=1700&&b<=1799)||(b>=2100&&b<=2199)||(b>=2500&&b<=2599))
    d=4;
    else if((b>=1800&&b<=1899)||(b>=2200&&b<=2299)||(b>=2600&&b<=2699))
    d=2;
    else if((b>=1900&&b<=1999)||(b>=2300&&b<=2399))
    d=0;
    else if((b>=2000&&b>=2099)||(b>=2400&&b<=2499))
    d=6;
    int e=(int)(c/4);
    for(int i=1;i<=12;i++)
    {
    switch(i)
    {
    case 10:
    x=0;
    break; case 1: if((year%4)==0) x=6; else x=0; break; case 2: if((year%4)==0) x=2;else x=3; break;
    case 3 : case 11:
    x=3;
    break;
    case 4: case 7:
    x=6;
    break;
    case 5 :
    x=1;
    break;
    case 6 :
    x=4; break;
    case 8 :
    x=2; break;
    case 12 : case 9 :
    x =5;
    break;}



    int z;

    z=((c+d+e+x+1)%7);

    print(i,z,year); //(Still Julian Calendar in Great Britain)
    }
    cout<<"\n\n PleAsE GrAde uS weLL "<<endl<<" ***********";
    return 0;
    }
    I have writen a code but there is a problem in this code .
    The code gives a true calender until 1999 but when it comes to the year 2000 the first day of the year is saturday but my code gives that the first day of the year is sunday ,, and so on for the other years that follows this year and I don't know really what is the problem.
    First I use Julian method to calculate the first day of the week ,and here is an example to illustrate this method.

    January 1, 2000

    Look up the 2000s in the centuries table: 6
    This is the table :
    1700–1799 4 (Still Julian Calendar in Great Britain and its colonies until 1752)
    1800–1899 2
    1900–1999 0
    2000–2099 6
    2100–2199 4
    2200–2299 2
    2300–2399 0
    2400–2499 6
    2500–2599 4
    2600–2699 2

    Note the last two digits of the year: 00
    Divide the 00 by 4: 0/4 = 0 and drop the fractional part: 0
    Look up January in the months table: 6 (leap)
    This is the table:
    January 0 (in leap year 6)
    February 3 (in leap year 2)
    March 3
    April 6
    May 1
    June 4
    July 6
    August 2
    September 5
    October 0
    November 3
    December 5

    Add all numbers from steps 1–4 to the day of the month (in this case, 19): 1+6+00+0+6=13.
    Divide the sum from step 5 by 7 and find the remainder: 13/7=1 remainder 6
    Find the remainder in the days table: 6=Saturday.
    This is the table:

    Sunday 0
    Monday 1
    Tuesday 2
    Wednesday 3
    Thursday 4
    Friday 5
    Saturday 6
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Feb 9, 2010 #2

    Mark44

    Staff: Mentor

    I'm pretty sure the following line of code is what is causing your problem. The inequality is going the wrong direction.
    Code (Text):

    else if((b>=2000&&b>=2099)||(b>=2400&&b<=2499))
     
    The first boolean expression should be b>=2000&&b<= 2099
    ......................................................................|........

    See if that fixes your problem.

    BTW, use [ code] and [ /code] tags (without leading spaces inside the brackets) to preserver your formatting and make the code more readable.
     
  4. Feb 9, 2010 #3
    ^^
    Yes, your are right ..
    merci beaucoup ...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ code, the calender
  1. Basic C++ Coding? (Replies: 3)

  2. Simple C coding (Replies: 14)

Loading...