#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int year, first, currentMonth=1;
cout << "Please enter a year for the calendar calculation: " << endl;
cin >> year;
cout << "Please enter the day of the week that January 1st occurs (as a number): " << endl;
cin >> first;
while (currentMonth <= 12); // Controls the current month.
{
int days;
int calculateDays(int currentMonth,int year); // Function that returns the number of days in the month
string monthTitle(int currentMonth);
cout << monthTitle << endl; // Prints the month title.
cout << "Sun\tMon\tTue\tWed\tThr\tFri\tSat\n"; // Prints the days of the week over the appropriate positions.
for (int j = 1; j < first; j++) //This loop sets the position of the first day of the month.
{
cout << " \t";
}
for (int tab = (1 + first); tab <= (first + days); tab++)
{
cout << (tab - first) << "\t";
if(tab % 7 == 0)
cout << endl;
}
first = first + (days % 7) + 1; // Used to calculate the amount of tabs for the next month.
cout << endl;
cout << endl;
++currentMonth;
}
return 0;
}
int calculateDays(int currentMonth,int year) // Uses the current month and year to calculate the number of days within the current month
{
int days;
switch(currentMonth)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
days = 31;
break;
case 4:case 6:case 9:case 11:
days = 30;
break;
case 2:
if ((year % 4 == 0) || (year % 400 == 0))
days = 29;
else
days = 28;
break;
}
return days;
}
string monthTitle(int currentMonth) // Used to determine the title of the current month
{
string monthTitle;
switch(currentMonth)
{
case 1: monthTitle = "January"; break;
case 2: monthTitle = "February"; break;
case 3: monthTitle = "March"; break;
case 4: monthTitle = "April"; break;
case 5: monthTitle = "May"; break;
case 6: monthTitle = "June"; break;
case 7: monthTitle = "July"; break;
case 8: monthTitle = "August"; break;
case 9: monthTitle = "September"; break;
case 10: monthTitle = "October"; break;
case 11: monthTitle = "November"; break;
case 12: monthTitle = "December"; break;
}
return monthTitle;
}
I know this program looks pretty convoluted, but I'm supposed to design a program that inputs a year and the day of the week that January 1st occurs on for that year (as a number, Sun=0, Mon=1, Tue=2, etc.) using what I've learned thus far from the class (which is basic syntax, function calling, if/while/for, switches, etc.). I made 2 functions, calculateDays is just a switch statement to assign the variable days the appropriate amount of days for whatever month, and monthTitle is another switch to get the title of the month.
The loops logic is a little hard to follow, but it all compiles with g++ in Linux. The issue I have is that, once it receives the inputs, the program just idles. I don't know why.
You have a ';' right after the while loop, remove it
1 2 3
while (currentMonth <= 12); // <------ Right here Controls the current month.
// Change it to
while (currentMonth <= 12) // <--- Remove the ';' Controls the current month.
int calculateDays(int currentMonth,int year); // Function that returns the number of days in the month
string monthTitle(int currentMonth);
you have NOT declared any variable for storing the returning values of the functions. you should declare in this way:
1 2 3
int numDays = calculateDays(int currentMonth,int year); //declare a variable 'numDays' of type 'int' and initialized with the return value of 'calculateDays()'
string monthName = monthTitle(int currentMonth); //declared a variable 'monthName' of type 'string' and initialized with the return value of 'monthTitle()'
cout << monthName << endl;
As Vins3Xtreme notes above the following (from lines 18 and 19 of your code) are function declarations and not function calls as you would like them to be.
1 2 3
int calculateDays(int currentMonth,int year); // Function that returns the number of days in the month
string monthTitle(int currentMonth);
cout << monthTitle << endl;
You already have a variable int days that is unused. I assume you meant that to hold the value returned from calculateDays. You only use the value returned from monthTitle to output immediately, so I don't see much point in storing it:
1 2
days = calculateDays(currentMonth, year);
cout << monthTitle(currentMonth) << endl;
Also, there is a logic error in line 56 of your code: years divisible by 400 are not leap years.