Hello all,
I am a beginner in c++ and started about 2 months ago. I am currently working on a project. The goal of my project is to write a program that when I enter the year/month the program will print a 30/31 day calendar grid for that month/year.
I have currently the functions for the leap year and months as per leap year but now I need help in writing the code necessary to actually print a calendar. Any help/tips on my code/post would be greatly appreciated. Please note that I must be able to achieve this with the structure provided. I have put a comment where it is that I am stuck. Thanks in advance!
#include <iostream>
#include <iomanip>
#include <string>
#define YEAR 1971
#define MONTH 1
using namespace std;
//
https://en.wikipedia.org/wiki/Leap_year
/*
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
*/
bool leapyear(int y){
if(y%4!=0) return false;
else if(y%100!=0) return true;
else if(y%400!=0) return false;
}
// Assume y>=1971
// Fact: Jan 1, 1971 is Friday.
int firstweekday(int y,int m){
//If it is Sunday, return 0. For Monday, return 1.
/* this counts up the days from 1968.12.31 which
is a Tuesday (2) */
int daynum = 0;
int dayofweek = 0;
daynum = 365*(y-1969)+((y-1969)/4);
dayofweek = (2+daynum)%7;
/* dayofweek is now the day of the week for
dec 31 of the previous year */
switch(m)
{
case 1: //jan 1st is one day after dec 31st
dayofweek+=1;
break;
case 2: //31 days later is feb 1st which is +3 mod 7
dayofweek+=4;
break;
case 3: //28 or 29 days later is mar 1st which is +0 or +1
if( leapyear(y) )
dayofweek+=5;
else
dayofweek+=4;
break;
case 4: //march has 31 days, mod 7 is 3
if( leapyear(y) )
dayofweek+=1;
else
dayofweek+=0;
break;
case 5: //april has 30 days, mod 7 is 2
if( leapyear(y) )
dayofweek+=3;
else
dayofweek+=2;
break;
case 6: //may has 31 days , mod 7 is 3
if( leapyear(y) )
dayofweek+=6;
else
dayofweek+=5;
break;
case 7: //june has 30 days, mod 7 is 2
if( leapyear(y) )
dayofweek+=1;
else
dayofweek+=0;
break;
case 8: //july has 31 days, mod 7 is 3
if( leapyear(y) )
dayofweek+=4;
else
dayofweek+=3;
break;
case 9: //august has 31 days, mod 7 is 3
if( leapyear(y) )
dayofweek+=0;
else
dayofweek+=6;
break;
case 10: //september has 30 days, mod 7 is 2
if( leapyear(y) )
dayofweek+=2;
else
dayofweek+=1;
break;
case 11: //october has 31 days, mod 7 is 3
if( leapyear(y) )
dayofweek+=5;
else
dayofweek+=4;
break;
case 12: //november has 30 days, mod 7 is 2
if( leapyear(y) )
dayofweek+=0;
else
dayofweek+=6;
break;
}
dayofweek%=7;
return dayofweek;
}
void showmonth(int y,int m){
//this is where I am stuck
}
int main() {
// Jan 1, 1971 is Friday
int y=1971, m=1 , dayofweek = 0;
cout<<"Type year(since 1971):";
cin>>y;
cout<<"Type month(from 1 to 12):";
cin>>m;
dayofweek = firstweekday(y,m);
cout << setw(2) << setfill('0') << 1 << endl;
//set up if statement to be <1 to show empty spaces
cout<< y << "." << m << " = " << dayofweek << endl;
showmonth(y,m);
return 0;
}