Help with getting my calendar to start on the right days.

I am attempting to make a calendar for my c++ class, but I can't seem to figure out what to do. Here is my code:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdlib>
#include <iostream>
using namespace std;

const int NUMBER_OF_MONTHS=12;

void printTitleOfCalendar(string months[],int i){
    cout<<"            "<<months[i]<<endl;
    cout<<" Sun  Mon  Tue  Wed  Thu  Fri  Sat \n";
}
void printDaysOfCalendar(int i,int daysOfMonth[],int j){
    for(i=1;i<=daysOfMonth[j];i++){
        if(i<=9)                                                        //Prints all 12 months of the year
            cout<<"  "<<i<<"  ";                                          
        if(i>9) 
            cout<<"  "<<i<<" ";
        if(i%7==0)
            cout<<endl<<endl;  
    }
}

void checkLeapYear(int userInput,int daysOfMonth[]){                                             //Check if the year is a leapyear
    if(userInput%4==0)
        daysOfMonth[1]=29;
    if(userInput%100==0)
        daysOfMonth[1]=28;
    if(userInput%400==0)
        daysOfMonth[1]=29;
}

void startDayOfMonth(int startDOW){
    if(startDOW==1)
        cout<<"     ";
        
     
                                                                          //Start on the right day of the month
     
}
int main(int argc, char *argv[]){
    int startDOW,userInput,i=0,j=0,daysOfMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
    string months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    
    cout<<"Enter in the year of the calendar: ";
    cin>>userInput;
    cout<<endl;
    checkLeapYear(userInput,daysOfMonth);
    
    startDOW=(userInput+(userInput-1)/4-(userInput-1)/100+(userInput-1)/400)%7;  //This is the algorithm that outputs a 0-6 for which day the month starts on
    
    
    for(i=0;i<NUMBER_OF_MONTHS;i++){
        printTitleOfCalendar(months,i);
        startDayOfMonth(startDOW);
        printDaysOfCalendar(i,daysOfMonth,i);
        
        cout<<endl<<endl;
    }
    
    cout<<startDOW;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I have the algorithm to see what day january starts on, startDOW outputs a number from 0-6 zero being sunday and six being saturday, but I just don't know what I could to. Can anyone help me?
Last edited on
I have played with calendars and it's tough. There are so many ways to do anything.

Here is a example of a really complex calendar, 1746 lines of code including comments.
// http://unicorn.us.com/cal.html

When determining time or dates, it is almost universally expected to be represented by the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC.

I have a couple examples to show

This one displays the current date/time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>      // Needed for printf()
#include <time.h>       // Needed for time data structures and functions
using namespace std;


int main()
{
  time_t timer;                // Define the timer
  struct tm *tblock;           // Define a structure for time block

  // Get time of day
  timer = time(NULL);

  // Converts date/time to a structure
  tblock = localtime(&timer);

  // Output ASCII data/time
  printf("Current time is: %s", asctime(tblock));

}


output:

Current time is: Thu Dec 13 11:46:17 2012


This one is not mine but shows each value, year, month, day, hour, etc...

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
32
#include <iostream>
#include <cstdlib>
#include <ctime>

int main(void) {
    time_t t;
    struct tm *tim;
    char tz[32];
    char ofs[32];

//    std::system ("date");
//    std::cout << std::endl;

    t = std::time (0);
    tim = std::localtime (&t);
    std::strftime (tz, sizeof (tz), "%Z", tim);
    std::strftime (ofs, sizeof (ofs), "%z", tim);

    std::cout << "Year:        " << (tim->tm_year + 1900) << std::endl;
    std::cout << "Month:       " << (tim->tm_mon + 1) << std::endl;
    std::cout << "Day:         " << tim->tm_mday << std::endl;
    std::cout << "Hour:        " << tim->tm_hour << std::endl;
    std::cout << "Minute:      " << tim->tm_min << std::endl;
    std::cout << "Second:      " << tim->tm_sec << std::endl;
    std::cout << "Day of week: " << tim->tm_wday << std::endl;  // Zero relative
    std::cout << "Day of year: " << tim->tm_yday << std::endl;
    std::cout << "DST?:        " << tim->tm_isdst << std::endl;
    std::cout << "Timezone:    " << tz << std::endl;
    std::cout << "Offset:      " << ofs << std::endl;

    return 0;
}


Output:

Year: 2012
Month: 12
Day: 13
Hour: 11
Minute: 44
Second: 35
Day of week: 4
Day of year: 347
DST?: 0
Timezone: Central Standard Time
Offset: Central Standard Time


I haven't done it yet, but I would guess there is a way to pass a different date than the current one to get the required info you want.

Here is one I wrote to play with the same concept. The nice thing about it compared to the others is it shows you the #Seconds, mins etc... I mainly did that to see that my math was correct, and as far as I know it is.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdio>
#include <time.h>
#include <math.h>

using namespace std;
int CSTTimeZone=6;
int main()
{
cout << "Times below are from 1/1/1970"<< endl;
time (0);
cout << "Seconds = " << time (0)<<endl;
cout << "Minutes = " << (time (0)/60)<<endl;
cout << "Hours   = " << (time (0)/60/60)<<endl;
cout << "Days    = " << (time (0)/60/60/24)<<endl;
cout << "Years   = " << (time (0)/60/60/24/365)<<endl;

// Determine if Leap Year
float LeapYearChk=(((time (0)/60/60/24/365)-2) % 4);

if (LeapYearChk==0)
{
     cout << "This is Leap year" << endl;
     if (((time(0)/60/60/24)-(((time(0)/60/60/24/365)*365)))>60)
     {
     cout << "Leapyear already occured this year"<< endl;
     cout << "There have been ";
     cout << ((((time (0)/60/60/24/365)+1970)-1972)/4)+1;
     cout << " Leap Years"  << endl;
     }
     else
     {
     cout << "Leapyear has not occured this year"<< endl;
     cout << "There have been ";
     cout << ((((time (0)/60/60/24/365)+1970)-1972)/4);
     cout << " Leap Years" << endl;
     }
}
cout << "------------------------"<< endl;
cout << "Current Year = " << ((time (0)/60/60/24/365)+1970) << endl;
cout << "Julian Date  = " <<((time(0)/60/60/24)-(((time(0)/60/60/24/366)*365))-(((((time (0)/60/60/24/365)+1970)-1972)/4))) << endl;
cout << "Current time = ";
cout << ((time (0)/60/60)-CSTTimeZone) %24  << ":" ;  // hours
cout << (time (0)/60) %60 << ":" ;  // minutes
cout << (time (0)) %60 << endl;  // seconds
return 0;
}


Output:

Times below are from 1/1/1970
Seconds = 1355420639
Minutes = 22590343
Hours = 376505
Days = 15687
Years = 42
This is Leap year
Leapyear already occured this year
There have been 11 Leap Years
------------------------
Current Year = 2012
Julian Date = 347
Current time = 11:43:59

Again, all these use the current date, but one of these might get you started to using a different date.
Last edited on
Topic archived. No new replies allowed.