Question regarding a calendar program

Hello everyone!

I've been reading this forum for several weeks now, trying to wrap my head around the world of c++ programming. I have found that this website is an excellent resource and I am grateful to all of you for taking the time to help noobs such as myself understand this stuff.

Anyway, my question is this. I am writing a calendar program for a class that I am taking. The program is about 90% done, however I am having a problem. My code is supposed to output a calendar for a specific month and year, and the code that I have written is generating a calendar for an entire year.

I'm just looking for pointers to steer me in the right direction. I'm assuming that it has something to do with the "string" statement in the printMonth section, but I am not sure how to remedy it. Any and all help is appreciated, thanks in advance!


Here is my code.

/*********************************************************/
#include <iostream>
#include <iomanip>
using namespace std;

void printmonth(const int month, const int startday, const bool leap);
void printyear(const int year);
int dayofweek(const int day, const int month, const int year);

/*********************************************************/

int main(void)
{
int month;
int year;
cout << "Enter a month number: ";
cin >> month;
cout << "Enter year: ";
cin >> year;

printyear(year);
}

void printyear(const int year)
{
bool leapyear = false;
if(year % 4 == 0)
{
if(year % 100 !=0)
leapyear = true;
}

int mon = 1;
for(mon = 1;mon < 13; mon++)
printmonth(mon,dayofweek(1,mon,year),leapyear);
}


//Finds what day of the week the year starts on

int dayofweek(const int day, const int month, const int year)
{
int a,y,m;
a=(14-month)/12;
y=year-a;
m= month+12*a-2;
return (day+y+y/4-y/100 + y/400 + (31*m/12))%7;
}
/***********************************************************************/

void printmonth(const int month, const int startday, const bool leap)
{

//Makes the arrays for the days of the week, the months,
//the days in each in month,and changes the days in Feb to
//29 if leap is true.

string Days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

string Months[13] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

int DaysinMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};


/**********************************************************************/

//Checks to see if it is a leap year and changes the days
//in February accordingly.

if(leap)
DaysinMonth[2] = 29;

// Finds the length of the name of the month,divides it by 2,
// and adds that length + 14 spaces to the beginning, centering it.

string str = Months[month];
int len = str.length();
cout << setw((len/2)+14) << Months[month] << endl;

// Adds four spaces for each day before startday

int i = 1;
int count = 0;
for(count = 0; count < startday ; count++)
{
cout << " ";
}

// Counts through the days of the month.

for(i=1;i<=DaysinMonth[month];i++)

{
// Outputs each days numerical value, checks to see
// if it is the end of the week, and goes to the next day

cout << setw(4) << i;
if((startday+i) % 7 == 0)
cout << endl;
}

cout << endl;

}

@TRL

The problem stemmed from your void printyear(const int year) function. You have a loop to cycle through all 12 months. I made a small change to the function so that it also passes the month variable and took out the loop. The only other problem I see, is that the first of the month is not being printed under the correct day location.
1
2
3
4
5
6
7
8
9
10
11
void printyear(const int year, const int month)
{
bool leapyear = false;
if(year % 4 == 0)
{
if(year % 100 !=0)
leapyear = true;
}

printmonth(month,dayofweek(1,month,year),leapyear);
}


To fix the day placement, make the cout in
1
2
3
4
for(count = 0; count < startday ; count++)
{
cout << " ";
}
to be 4 spaces wide, instead of 1.
Last edited on
Thank you whitenite! You have helped me out a great deal!
Topic archived. No new replies allowed.