Hi all. Eventually my program needs to prompt you to enter a year between 1800-2007 and then it will print off the calendar for that year. In order to do this I must create functions that determine what day of the week Jan 1 was that year, and a function that prints a header for each month and a function that prints off the days of that month. For now, I just said the first day of each month was a Wednesday and that every month has 30 days. Before I figure out how to return the correct amount of days for each month and throw leap years into the equation, I would like help with a much simpler portion of my program. I can't get my program to print a different header for each month. On the very bottom of my program, where I have: void Print_Month_Head(int m) if I just type cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n"; My program will print a header for January on top of every month. So I tried implementing a for loop for 1<m<12 with an 'if' statement that lists a different cout prompt for every value of m, but when i run the program it just lists the header for january an infinite amount of times. My entire code is listed below, any help I can receive would be great, thanks!

#include <iostream>;
#include <iomanip>;

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int First_Day_Of_Month(int y, int m);
int Number_Days_Of_Month(int y, int m);
void Print_Version();
void Print_Head(int y);
void Print_Month(int y, int m);
void Print_Month_Head(int m);


void main ()
{
    Print_Version();
        int year;
        cin >> year;

    Print_Head(year);
    for(int i=1; i<=12; i++){
        Print_Month(year, i);
    }
    cout << "bye";
}

void Print_Version()
{
    cout << "Welcome \n";
}

void Print_Head(int y)
{
    cout << "                         " << y << endl;
    cout << "==================================================\n";
}

void Print_Month(int y, int m)
{
    Print_Month_Head(m);
    int firstday, number_days;
    firstday = First_Day_Of_Month(y,m);
    number_days = Number_Days_Of_Month(y,m);
    cout << "            ";
    for (int k=0; k<firstday; k++)
        cout << "     ";
    for (int i = 1; i<number_days; i++){
        cout << setw(5) << i;
        if ((i + firstday)%7 == 0){
            cout << endl;
            cout << "            ";
        }
    }
}

int First_Day_Of_Month(int y, int m)
{
    return 2;
}

int Number_Days_Of_Month(int y, int m)
{
    return 30;
}

void Print_Month_Head(int m)
{    
    for (int i = 1; i<=12; i++)
    {
        if (i = 1)
        {
            cout << "\n January      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 2)
        {
            cout << "\n February     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 3)
        {
            cout << "\n March        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 4)
        {
            cout << "\n April        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 5)
        {
            cout << "\n May          Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 6)
        {
            cout << "\n June         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 7)
        {
            cout << "\n July         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 8)
        {
            cout << "\n August       Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 9)
        {
            cout << "\n September    Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 10)
        {
            cout << "\n October      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 11)
        {
            cout << "\n November     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else
        {
            cout << "\n December     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }

    }
}

Okay, first thing's first. Print_Month_Head shouldn't be looping. You only want one header, so all you need to do is test m once and print whatever. That fixes the problem that you'll get after you solve the problem that you have now. :)

The problem that you have now is an infinite loop. I'm sure you're surprised to hear that. ;) The reason you have an infinite loop is that every test in the if chain for Print_Month_Head doesn't do a comparison. It does an assignment. You use = to set a value and == to test a value. Or as I like to say, once to set it, twice to make sure. :)

If you change = to == and remove the loop in Print_Month_Head, you'll have something that looks more like a calendar. :)

Thanks! Yes I could tell it was an infinite loop and figured it had something to do with the for loop. So i took out the for loop and added all the '=='. Now I am trying to determine the number of days to print off per month. I believe I got every month all set except for February (because of leap years). What I was thinking of doing was making another function:

bool IsLeapYear(int y)
                   {
                      if  (the year is divisible by 400)
                          it is a leap year;
                      else if (the year is divisible by 4, but not 100)
                          it is a leap year;
                      else
                          it is not a leap year;

But I am unclear as to how I actually program this function. I know that I can then do something like

if IsLeapYear(y) == 1
                   return 30;
                else 
                   return 29;

Here is my updated code thus far:

#include <iostream>;
#include <iomanip>;

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int First_Day_Of_Month(int y, int m);
int Number_Days_Of_Month(int y, int m);
void Print_Version();
void Print_Head(int y);
void Print_Month(int y, int m);
void Print_Month_Head(int m);


void main ()
{
    Print_Version();
        int year;
        cin >> year;

    Print_Head(year);
    for(int i=1; i<=12; i++){
        Print_Month(year, i);
    }
    cout << "bye";
}

void Print_Version()
{
    cout << "Welcome \n";
}

void Print_Head(int y)
{
    cout << "                         " << y << endl;
    cout << "==================================================\n";
}

void Print_Month(int y, int m)
{
    Print_Month_Head(m);
    int firstday, number_days;
    firstday = First_Day_Of_Month(y,m);
    number_days = Number_Days_Of_Month(y,m);
    cout << "            ";
    for (int k=0; k<firstday; k++)
        cout << "     ";
    for (int i = 1; i<number_days; i++){
        cout << setw(5) << i;
        if ((i + firstday)%7 == 0){
            cout << endl;
            cout << "            ";
        }
    }
}

int First_Day_Of_Month(int y, int m)
{
    return 2;
}

int Number_Days_Of_Month(int y, int m)
{
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        return 32;
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        return 31;
    }
    else
        return 29;
}

bool IsLeapYear(int y)
{

void Print_Month_Head(int m)
{    
    
    
        if (m == 1)
        {
            cout << "\n January      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 2)
        {
            cout << "\n February     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 3)
        {
            cout << "\n March        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 4)
        {
            cout << "\n April        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 5)
        {
            cout << "\n May          Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 6)
        {
            cout << "\n June         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 7)
        {
            cout << "\n July         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 8)
        {
            cout << "\n August       Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 9)
        {
            cout << "\n September    Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 10)
        {
            cout << "\n October      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (m == 11)
        {
            cout << "\n November     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else
        {
            cout << "\n December     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
    
    


}

I just realized why I had to put an extra day for each month in my code before. It was because in the main function i<number_days needed to be changed to <=number_days. So I fixed that and all the numbers in the Number_of_Days function. But I still need help with this boolean function problem, thanks!

I created a boolean function listed below:

bool IsLeapYear(int y) 
{
    if (y%400 == 0)
    {
        return 1;
    }
    else if (y%4 == 0 && y%100 != 0)
    {
        return 1;
    }
    else
        return 0;
}

I then updated my code for Number_of_days as listed below:

int Number_Days_Of_Month(int y, int m)
{
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        return 31;
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        return 30;
    }
    else if (IsLeapYear)
    {
        return 29;
    }
    else
    {
        return 28;
    }
    
}

But when I run my program, no matter what year I type, February always has 29 days. Why would this be? I am reattaching my entire code at the bottom of this message, thanks!!

#include <iostream>;
#include <iomanip>;

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int First_Day_Of_Month(int y, int m);
int Number_Days_Of_Month(int y, int m);
bool IsLeapYear(int y);
void Print_Version();
void Print_Head(int y);
void Print_Month(int y, int m);
void Print_Month_Head(int m);


void main ()
{
    Print_Version();
        int year;
        cin >> year;

    Print_Head(year);
    for(int i=1; i<=12; i++){
        Print_Month(year, i);
    }
    cout << "\nGoodbye! \n";
}

void Print_Version()
{
    cout << "Please Enter any Year After 1799 \n";
}

void Print_Head(int y)
{
    cout << "                         " << y << endl;
    cout << "==================================================\n";
}

void Print_Month(int y, int m)
{
    Print_Month_Head(m);
    int firstday, number_days;
    firstday = First_Day_Of_Month(y,m);
    number_days = Number_Days_Of_Month(y,m);
    cout << "            ";
    for (int k=0; k<firstday; k++)
        cout << "     ";
    for (int i = 1; i<=number_days; i++){
        cout << setw(5) << i;
        if ((i + firstday)%7 == 0){
            cout << endl;
            cout << "            ";
        }
    }
}

bool IsLeapYear(int y) 
{
    if (y%400 == 0)
    {
        return 1;
    }
    else if (y%4 == 0 && y%100 != 0)
    {
        return 1;
    }
    else
        return 0;
}

int First_Day_Of_Month(int y, int m)
{
    return 2;
}

int Number_Days_Of_Month(int y, int m)
{
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        return 31;
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        return 30;
    }
    else if (IsLeapYear)
    {
        return 29;
    }
    else
    {
        return 28;
    }
    
}



    

void Print_Month_Head(int m)
{    
    
    
    if (m == 1)
    {
        cout << "\n January      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 2)
    {
        cout << "\n February     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 3)
    {
        cout << "\n March        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 4)
    {
        cout << "\n April        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 5)
    {
        cout << "\n May          Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 6)
    {
        cout << "\n June         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 7)
    {
        cout << "\n July         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 8)
    {
        cout << "\n August       Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 9)
    {
        cout << "\n September    Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 10)
    {
        cout << "\n October      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 11)
    {
        cout << "\n November     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else
    {
        cout << "\n December     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    
}

You aren't calling the function with an argument. This is something your compiler should be wraning you about.

else if (IsLeapYear)

It should be

else if (IsLeapYear( y ))

Thanks Ravalon! What I actually did instead was:

int Leapyear;
    Leapyear = IsLeapYear(y);
   else if (Leapyear == 1)

and the code works just fine! Now the only (and biggest) kink I have left in my code is determining the first day of every month. The information I was given was:

First day of January in 1800 was a Wednesday. Then adding up all the days up to this given year(including extra days from leap years) and finding the remainder of 7. this supposedly helps me determine the day of the week for the first day of every month. I am unclear as to how to type a code for something like this, it seems like it would be a lot of 'if' statements. I am going to try and sit and think about this but I was wondering if you could give me some ideas to work with while developing this, because it is giving me a lot of trouble. Thanks a lot! As always, my code is posted below:

#include <iostream>;
#include <iomanip>;

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int First_Day_Of_Month(int y, int m);
int Number_Days_Of_Month(int y, int m);
bool IsLeapYear(int y);
void Print_Version();
void Print_Head(int y);
void Print_Month(int y, int m);
void Print_Month_Head(int m);


void main ()
{
    Print_Version();
        int year;
        cin >> year;

    Print_Head(year);
    for(int i=1; i<=12; i++){
        Print_Month(year, i);
    }
    cout << "\nGoodbye! \n";
}


//Functions

void Print_Version()
{
    cout << "Please Enter any Year After 1799 \n";
}

void Print_Head(int y)
{
    cout << "                         " << y << endl;
    cout << "==================================================\n";
}

void Print_Month(int y, int m)
{
    Print_Month_Head(m);
    int firstday, number_days;
    firstday = First_Day_Of_Month(y,m);
    number_days = Number_Days_Of_Month(y,m);
    cout << "            ";
    for (int k=0; k<firstday; k++)
        cout << "     ";
    for (int i = 1; i<=number_days; i++){
        cout << setw(5) << i;
        if ((i + firstday)%7 == 0){
            cout << endl;
            cout << "            ";
        }
    }
}

bool IsLeapYear(int y) 
{
    if (y%400 == 0)
    {
        return 1;
    }
    else if (y%4 == 0 && y%100 != 0)
    {
        return 1;
    }
    else
        return 0;
}

int First_Day_Of_Month(int y, int m)
{
    return 2;
}

int Number_Days_Of_Month(int y, int m)
{
    int Leapyear;
    Leapyear = IsLeapYear(y);
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
    {
        return 31;
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        return 30;
    }
    else if (Leapyear == 1)
    {
        return 29;
    }
    else
    {
        return 28;
    }
    
}

void Print_Month_Head(int m)
{    
    
    if (m == 1)
    {
        cout << "\n January      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 2)
    {
        cout << "\n February     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 3)
    {
        cout << "\n March        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 4)
    {
        cout << "\n April        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 5)
    {
        cout << "\n May          Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 6)
    {
        cout << "\n June         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 7)
    {
        cout << "\n July         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 8)
    {
        cout << "\n August       Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 9)
    {
        cout << "\n September    Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 10)
    {
        cout << "\n October      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else if (m == 11)
    {
        cout << "\n November     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    else
    {
        cout << "\n December     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
    }
    
}

First day of January in 1800 was a Wednesday. Then adding up all the days up to this given year(including extra days from leap years) and finding the remainder of 7. this supposedly helps me determine the day of the week for the first day of every month. I am unclear as to how to type a code for something like this, it seems like it would be a lot of 'if' statements. I am going to try and sit and think about this but I was wondering if you could give me some ideas to work with while developing this, because it is giving me a lot of trouble. Thanks a lot!

You might like this one. :) yy is the year, mm is the month, dd is the day.

y = yy - (14 - mm) / 12
m = mm + 12 * ((14 - mm) / 12) - 2

Now take a deeeeeep breath.

d = (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12) % 7

Whew! And now d contains the numeric value of the day, starting at 0 with Sunday. No if statements needed. :) My best advice is never to take the obvious solution without first trying to think of other ways around a problem. If you're not a mathematics buff, something like this also strikes me as a good problem for a lookup table. You can precalculate by hand the first day of each month for a millenia or so and put them all in an array. That's less than 100 values, so it's still a viable option.

You might like this one. :) yy is the year, mm is the month, dd is the day.

y = yy - (14 - mm) / 12
m = mm + 12 * ((14 - mm) / 12) - 2

Now take a deeeeeep breath.

d = (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12) % 7

Whew! And now d contains the numeric value of the day, starting at 0 with Sunday. No if statements needed. :) My best advice is never to take the obvious solution without first trying to think of other ways around a problem. If you're not a mathematics buff, something like this also strikes me as a good problem for a lookup table. You can precalculate by hand the first day of each month for a millenia or so and put them all in an array. That's less than 100 values, so it's still a viable option.

Ravalon,
I was wondering if you could clarify what exactly 'd' is for me. Also let me clarify what this firstday function must do. Basically, firstday must tell the program how many days to indent for the first week for each month. So if the first day of January in 1800 is a Wednesday, firstday must indent 2 spaces (return 2) If the first day of a month is a Sunday, then it must return 0;

I am just completely unclear as to how I need to even write this function. I was hoping there would be some sort of pattern that I could use if statements that returns a different amount of spaces depending on the year and month.

So I was wondering how you came up with your equations above, and what they mean exactly, esp 'd'. And also, I am curious as to how I actually implement these equations into my program. It was suggested to us (I forgot to mention earlier) that we should give int m a default value in the function header:

in FirstDay (int y, int m = 1)

Also, the user only enters a year, so I don't know how to implement the y and m equations you provided me. Would it just be one large equation for 'd', and then finish it off with: return d; at the bottom? Thanks again Ravalon, you've been a big help!

Ravalon,
I was wondering if you could clarify what exactly 'd' is for me.

It's a variable that you define to hold the value of the weekday. 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.

So I was wondering how you came up with your equations above, and what they mean exactly, esp 'd'.

Well, like any good programmer I stole them. :cheesy:

And also, I am curious as to how I actually implement these equations into my program.

They're valid C++ expressions. Try just tossing them into a function and see what you get.

int dayOfWeek( int dd, int mm, int yy )
{
  int y = yy - (14 - mm) / 12;
  int m = mm + 12 * ((14 - mm) / 12) - 2;
  return (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
}

If you know the day of the week for any given date, all you need to do is give the first day of the month and your problem is solved. :)

Also, the user only enters a year, so I don't know how to implement the y and m equations you provided me.

You only need the year. There are only twelve months as far as I know, and I'm pretty sure that the first day of every month is the 1st. ;)

If you know the day of the week for any given date, all you need to do is give the first day of the month and your problem is solved. :)

You only need the year. There are only twelve months as far as I know, and I'm pretty sure that the first day of every month is the 1st. ;)

I'm still confused how to implement this. so that would mean I just have to initialize something? I tried the following:

int First_Day_Of_Month(int y, int m=1)
{
    int dayOfWeek( int dd, int mm, int yy )
    {
        int y = yy - (14 - mm) / 12;
        int m = mm + 12 * ((14 - mm) / 12) - 2;
        return (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
    }
    return dayOfWeek;
}

but it gives me the following errors:

error C2601: 'dayOfWeek' : local function definitions are illegal
        error C2440: 'return' : cannot convert from 'int (__cdecl *)(int,int,int)' to 'int'
        There is no context in which this conversion is possible

I know you said that if I know the first day of the month my problem should be solved, so i have to set dayofWeek = 1?

I'm still confused how to implement this. so that would mean I just have to initialize something? I tried the following:

int First_Day_Of_Month(int y, int m=1)
{
    int dayOfWeek( int dd, int mm, int yy )
    {
        int y = yy - (14 - mm) / 12;
        int m = mm + 12 * ((14 - mm) / 12) - 2;
        return (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
    }
    return dayOfWeek;
}

but it gives me the following errors:

error C2601: 'dayOfWeek' : local function definitions are illegal
        error C2440: 'return' : cannot convert from 'int (__cdecl *)(int,int,int)' to 'int'
        There is no context in which this conversion is possible

The error says what it means. You can't nest function definitions. Don't get used to it, most errors are much more cryptic. ;) First_Day_Of_Month should be calling the function instead.

int First_Day_Of_Month(int y, int m=1)
{
    return dayOfWeek( 1, m, y );
}

The error says what it means. You can't nest function definitions. Don't get used to it, most errors are much more cryptic. ;) First_Day_Of_Month should be calling the function instead.

int First_Day_Of_Month(int y, int m=1)
{
    return dayOfWeek( 1, m, y );
}

Thanks! it works fine now!

Hi,

This is garni, you can use the below code to determine the particular day in a week, what i have coded for you.

Input parameters: year, month and day.

return value: corresponding day string.

for this you need to include the header file "time.h"

& tested this function with Turbo C++ version 3.0, it worked fine.

char* FindDay(int year,int month,int day)
{
    char *wday[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
                    "Thursday", "Friday", "Saturday", "Unknown"};
    struct tm time_check;
    time_check.tm_year = year - 1900;
    time_check.tm_mon = month - 1;
    time_check.tm_mday = day;
    time_check.tm_hour = 0;
    time_check.tm_min = 0;
    time_check.tm_sec = 1;
    time_check.tm_isdst = -1;
    if (mktime(&time_check) == -1)
        time_check.tm_wday = 7;
    return wday[time_check.tm_wday];
}

thx,
Garni.

& tested this function with Turbo C++ version 3.0, it worked fine.

Yep, if it compiles with the old crappy Turbo, the code must be fine. :rolleyes:

  • You didn't use code tags
  • Stop using ugly colors
  • This function has nothing to do with the OP's question
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.