Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,157 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,132 people online right now. Registration is fast and FREE... Join Now!




Ouptutting calendar for any given month/year

 
Reply to this topicStart new topic

Ouptutting calendar for any given month/year, I need help writing a program in C++ that will output any given month

helpmeplease!
5 Dec, 2007 - 08:21 PM
Post #1

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 2


My Contributions
I have to write a program that will output any month for any year from 1900-now. I need functions that include corrections that need to be made for leap years, as well as functions that calculate what day of the month to start on. I am really lost and would appreciate any help. Here is the code I have so far:

CODE
#include <iostream>
#include <iomanip>
using namespace std;

//FUNCTIONS

int IsLeapYear(int year)
{
    int leap, year;
    if(year%400=0)
    {
        leap=1; //is leap year
    }
    else
    {
        leap=0; //is not leap year
    }
}
int DaysInMonth (int month)
{
    int days[]={31,28,31,30,31,30,31,31,310,31,30,31}
    return days[month];
}

int PrintCalendar (int  
{
if (month==1)
        cout << "January  ";
    else if (month==2)
        cout << "February  ";
    else if (month==3)
        cout << "March  ";
    else if (month==4)
        cout << "April  ";
    else if (month==5)
        cout << "May  ";
    else if (month==6)
        cout << "June  ";
    else if (month==7)
        cout << "July  ";
    else if (month==8)
        cout << "August  ";
    else if (month==9)
        cout << "September  ";
    else if (month==10)
        cout << "October  ";
    else if (month==11)
        cout << "November  ";
    else if (month==12)
        cout << "December  ";
    cout << setw(2) << year;
    cout << endl << endl;
    cout << "Sun  " << setw(2) << "Mon  " << setw(2) << "Tues  " << setw(2) << "Wed  " << setw(2) << "Thurs  " << setw(2) << "Fri  " << setw(2) << "Sat  " << endl;




int PrintDay(int day)
{
    for (days[i])
    {
        int day = StartDay;
        cout << num;
        day++;
        if (day > 7)
        {
            cout << endl;
            day = 1;
        }
    }
}

int FirstDay(days[])
{
    const int Jan_Num=31;
    int first_dat, int Jan_Num, int tot_m2, int month;

    if (month==1)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==2)
        tot_m2=28*(year-1900)+(365*(year-1900));
    else if (month==3)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==4)
        tot_m2=30*(year-1900)+(365*(year-1900));
    else if (month==5)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==6)
        tot_m2=30*(year-1900)+(365*(year-1900));
    else if (month==7)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==8)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==9)
        tot_m2=30*(year-1900)+(365*(year-1900));
    else if (month==10)
        tot_m2=31*(year-1900)+(365*(year-1900));
    else if (month==11)
        tot_m2=30*(year-1900)+(365*(year-1900));
    else if (month==12)
        tot_m2=31*(year-1900)+(365*(year-1900));
    first_day=(1+(tot_m1+tot_m2))%7;
}
void main ()
{
    int month, year;
    cout << "Enter month (1-12) or 0 to quit: ";
    cin >> month;
    cout << "Enter year (1900 or above): ";
    cin >> year;
    
}





User is offlineProfile CardPM
+Quote Post

htsh_shrm
RE: Ouptutting Calendar For Any Given Month/year
5 Dec, 2007 - 10:26 PM
Post #2

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
see first of all you should take care that 1st jan 1900 was monday.
then rather then calculating the no. of days for each month from jan ,feb.........dec. just calculate the no. of days for months having 30 days & for months havin 31 days & seperately for the month of february...........
i can give u the algorithm for the same but i would like you to do this urself........
but if u need any help i`ll help you coz i hav a huge programme on calendar which i made myself few months back so it can solve any of ur problems very easily........
User is offlineProfile CardPM
+Quote Post

helpmeplease!
RE: Ouptutting Calendar For Any Given Month/year
6 Dec, 2007 - 05:22 PM
Post #3

New D.I.C Head
*

Joined: 5 Dec, 2007
Posts: 2


My Contributions

ok, so I've been working for hours on this thing and my biggest struggle is understanding the formula for calulated the first day of the month. I just don't even understand how that's possible. I've put data about each month into arrays, therefore shortening the code. the formula I currently have for calculating each day of the month is this:
(this is the code for Jan & Feb.)
[code]if (month==1)
tot_m2=31*(year-1900)+(365*(year-1900));
else if (month==2)
tot_m2=28*(year-1900)+(365*(year-1900)); [\code]

I'm also have a hard time grasping the concept of calling functions. for instance, if I have an IsLeapYear function that will set the variable leap to 1 if the given year is a leap year, how do I call that function without it outputting a "1" onto the screen. I want the function to "run" and figure out if the year is a leap year, but I don't want it to output anything, and I don't understand how to call it. Here is my code for that function:
[code]int IsLeapYear(int year)
{
int leap, year;
if(year%400=0 || (year%4==0 && year%100!=0))
{
leap=1; //is leap year
}
else
{
leap=0; //is not leap year
}
}[\code]

if anyone could help me figure out this formula or could help me understand how to call a function, I will give you $100. not really, but I really would appreciate the help.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Ouptutting Calendar For Any Given Month/year
6 Dec, 2007 - 06:27 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Consider, you need three bits of info to do your calendar, beyond the given year and month.

You need to figure the day of the week and the number of days for the month. For the number of days in the month, you need to know if it's a leap year or not.

It's no real secret, I found this on the very first page of a google search: http://c-faq.com/misc/zeller.html

Ok, IsLeapYear should return boolean for maximum ease of use. So:
CODE

bool IsLeapYear(const int year) {
   return ((year%400==0) || (year%4==0 && year%100!=0));
}


Now that we have a working IsLeapYear, time for a GetDaysInMonth, something like this:
CODE

int GetDaysInMonth (const int year, const int month) {
   switch (month) {
      case 2:
     return IsLeapYear(year) ? 29 : 28;
      case 4:
      case 6:
      case 9:
      case 11: return 30;
      default: return 31;
  }  
}


Now all we need is day of week, from the link:
CODE

int GetDayOfWeek(const int year, const int month) {    /* 0 = Sunday */
   const int d = 1;
   int y = year - (month < 3);
   static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
   return (y + y/4 - y/100 + y/400 + t[month-1] + d) % 7;
}


That's it, you're done!

Well, I guess we've made it this far... here's the rest, enjoy.
CODE

void ShowCal(const int year, const int month) {
   string monthName[] = {"January","February","March","April","May","June","July",
   "August","September","October","November","December"};
  
   int days = GetDaysInMonth(year, month);
   int dow = GetDayOfWeek(year, month);
   cout << monthName[month-1] << endl;
   cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
   cout << " --- --- --- --- --- --- ---" << endl << " ";
   for(int day=0; day<dow; day++) { cout << "    "; }
   for(int day=1; day<=days; day++) {
      cout << setw(3) << day << " ";
      if (++dow>6) { dow = 0; cout << endl << " "; }
   }
   cout << endl;
}


This post has been edited by baavgai: 6 Dec, 2007 - 06:27 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month