Generate Current Month Calendar and Add/Show Reminder in C++

Views: 7119
Comments: 1
Like/Unlike: 1
Posted On: 02-Oct-2017 03:04 

Share:   fb twitter linkedin
reena
122 Points
12 Posts

Introduction

Write a program in C++, to generate calendar of current month with reminder. Also user can add reminder event. In this article we will learn how to generate calendar in current of current and then try to add reminder in calendar.

Detail example

#include<iostream>
#include <ctime>
#include<conio.h>
#include<string.h>
using namespace std;

//function will return total number of days
int  getNumberOfDays(int month, int year)
{
//leap year condition, if month is 2
if( month == 2)
{
   if((year%400==0) || (year%4==0 && year%100!=0))
     return 29;
   else
     return 28;
}
//months which has 31 days
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
||month == 10 || month==12)
   return 31;
else
   return 30;
}

void printCalendar(const char** weekDays, int firstWeekDayOfMonth, int numberOfDays, int remiderDay){
int w,d;
for( w=0; w < 7;w++){
   cout << weekDays[w] << "    ";
}
cout << "\n-------------------------------------------------\n";
cout << "\n";
int tempBreak=1;

for(w=0;w<firstWeekDayOfMonth;w++){
   cout << "       ";
   tempBreak++;
}
for(d=1;d<=numberOfDays;d++){
   if(d==remiderDay) {
     cout << "*"<< d << "*"<<"   ";
   } else{
     cout << d <<"     ";
   }

if(d<10)
   cout << " ";
if(tempBreak>=7)
{
   cout << "\n";
   tempBreak=0;
}
tempBreak++;
}
}

int main()
{
int i, w, d, n, j, c = 1;
const char * months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
const char * weekDays[7]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
int currentDay=now->tm_mday;
int weekDay=now->tm_wday;
int currentYear=now->tm_year;
int currentmonth=now->tm_mon;
int numberOfDays=getNumberOfDays(now->tm_mon + 1, now->tm_year);    
    
tm tFirst = { 0, 0, 0,1, currentmonth, currentYear };
time_t time_temp = mktime(&tFirst);
struct tm * firstTime = localtime(&time_temp);  
  
int firstWeekDayOfMonth=firstTime->tm_wday;
        
cout << "Current Date: " << firstTime->tm_mday << "-"<< months[firstTime->tm_mon] << "-" << firstTime->tm_year + 1900<<"\n\n";
cout << months[currentmonth] << "-" << currentYear + 1900<<"\n\n";
    
printCalendar(weekDays, firstWeekDayOfMonth, numberOfDays, 0);
  
cout << "\n";
cout << "Press enter key to add reminder\n";
getch();


int dayReminder;
char remiderMessage[100];

cout << "Enter reminder day: ";
cin >> dayReminder;
cout << "Enter reminder message:";
cin >> remiderMessage;

cout << "\n\n";

printCalendar(weekDays, firstWeekDayOfMonth, numberOfDays, dayReminder);

cout << "\n==================Reminder=====================\n";
cout << "Reminder day:" << dayReminder;
cout << "\n" << remiderMessage;
getch();

return 0;
}

Output

 

Conclusion

We will see the basics for generating current month calendar and how to add remider in it. Hope it will help.

1 Comments
please how do I change the date

ezek
18-Jun-2020 at 03:59
 Log In to Chat