Any ideas of how I can have a calendar for all 12 months print for any year?
The user inputs the year and the day of the week for January 1st of that year. (Sunday = 0, Monday = 1, etc.)

Important:
I cannot use cases. (That's what I've mostly seen online to solve this problem)
I can only use <iostream>, no other libraries.
And I am supposed to use two for loops.

Could someone provide a pseudocode or any suggestions?

Recommended Answers

All 10 Replies

Well, you know how many days are in each month, and what weekday the year starts, so the algorithm stands out immediately as a couple of nested loops with a few counters that wrap around at appropriate times. You're looking at around 10 lines of code.

Don't over complicate things, this is almost a no brainer. Of course handling leap years and error handling the weekday input are both minor complications, but it's not a big deal. See if you can come up with an algorithm on your own.

So far, I have the beginning of the program and then a few random pieces lol...

#include <iostream>
using namespace std;




int main()
{
int year;
int day;
int month = 1;
int i;

cout << "Calendar Program" << endl << endl;
cout << "Please enter the year: ";
cin >> year;
if (year < 0)
{cout << "The year must be greater than or equal to 0!" << endl;
return 0;}
cout << "Please enter the day of the week of January 1st." << endl;
cout << "Enter 0 for Sunday, 1 for Monday, ..., 6 for Saturday: ";
cin >> day;
if (day < 0 || day > 6)
{cout << "The day of January 1st must be between 0 and 6!" << endl;
return 0;}

cout << endl << endl;






//random pieces


for (int i = 1; i < day; ++ i) 
{cout << " ";}                //This is for the spaces at the beginning of the month (if the first day isn't a Sunday)



for (month = 1; month <= 12; month++) //print out months
{ if (month == 1)
{cout << "January";}
if (month == 2)
{cout << "February";} //etc

}

for (year = 1; year <= 100000000; year++) //leap year
{
if (year % 4 == 0 && year % 100 != 0)
{}                                      //print February with 29 days
return 0;}



}





return 0;
}

Am I on the right track at least? I still have no idea how to actually print a month.

Am I on the right track at least?

Yes.

I still have no idea how to actually print a month.

Here's an example of what I was talking about:

#include <iomanip>
#include <iostream>

using namespace std;

int main(void)
{
    const int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int wday = 3;

    // Print this year's calendar
    for (int month = 0; month < 12; ++month) {
        // Print leading blanks for last month
        for (int i = 0; i < wday; ++i)
            cout << setw(3) << "";

        // Print this month's calendar
        for (int mday = 0; mday < mdays[month]; ++mday, ++wday) {
            if (wday == 7) {
                cout << '\n';
                wday = 0;
            }

            cout << setw(3) << mday + 1;
        }

        // Set up for the next month
        if (wday != 7) {
            // Avoid one too many blank lines if the 
            // month ends a linebreaking weekday
            cout << '\n';
        }

        cout << endl;
    }
}

Is there a way without using #include <iomanip>? We haven't learned that yet

You don't need the iomanip header file. It just allows you to format your code nicely. So to make the above code work, just remove functions like setw(number_here) and it should compile.

Oh okay, thank you

Okay, I made more progress. And decided to use the iomanip :p
This is what I get: http://i.imgur.com/9ypos.jpg
Can someone please tell me how to get the month names all aligned to the left? And how to fix the alignment of the first week?

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




int main()
{
int mdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int DaysinWeek = 0;
int year;
int day;
int FirstDayOfMonth = 0;
int month = 1;



cout << "Calendar Program" << endl << endl;
cout << "Please enter the year: ";
cin >> year;
if (year < 0)
{cout << "The year must be greater than or equal to 0!" << endl;
return 0;}
cout << "Please enter the day of the week of January 1st." << endl;
cout << "Enter 0 for Sunday, 1 for Monday, ..., 6 for Saturday: ";
cin >> day;
if (day < 0 || day > 6)
{cout << "The day of January 1st must be between 0 and 6!" << endl;
return 0;}

cout << endl << endl;






// Print this year's calendar
for (int month = 0; month < 12; ++month) {
{

for (int i = 0; i < DaysinWeek; ++i)
{cout << setw(3) << " ";}


if (month == 0)
{cout << "January \n";}

if (month == 1)
{cout << "February \n";
 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
mdays[1] = 29;}

if (month == 2)
{cout << "March \n";}

if (month == 3)
{cout << "April \n";}

if (month == 4)
{cout << "May \n";}

if (month ==5)
{cout << "June \n";}

if (month == 6)
{cout << "July \n";}

if (month == 7)
{cout << "August \n";}

if (month == 8)
{cout << "September \n";}

if (month == 9)
{cout << "October \n";}

if (month == 10)
{cout << "November \n";}

if (month == 11)
{cout << "December \n";}

}


//spaces
for (int i = 0; i < day; i++)
    {cout << "  ";

}

  //Determine where the first day begins
day = 0;
    for (FirstDayOfMonth; FirstDayOfMonth < day; ++FirstDayOfMonth)
    {   
        cout << setw(3);
    } 








// Print this month's calendar
for (int mday = 0; mday < mdays[month]; ++mday) {
cout << setw(3) << mday + 1;
DaysinWeek++;

if (DaysinWeek == 7) {
cout << "\n" << setw(3);
DaysinWeek = 0;
}

}

// Set up for the next month
if (DaysinWeek != 7) {
cout << "\n";
}

cout << endl;
day = DaysinWeek + 1;
}



return 0;
}

I have a feeling that there should be a part that says:
If the spaces + DaysinWeek == 7, "\n". Is that right? I can't figure out where to put it though...

Do I need to create another function(s)?

hey can you please help me out! do you still have this file completed anywhere? please let me know. i would love to see it. i have the same problem and i cannot get numbers to loop after they hit saturday they just go on forever. Please help me. thanks

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.