Calendar Program

I'm writing a calendar program that ask for the the number of the month and the year and displays a calendar. My problem is that when it tries to print the calendar but the infinitely loops the numbers, I can't get the offset right, nether can I get the calendar header to display the right month or year.

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

int daysYear(int yearNumber);
int daysMonth(int monthNumber);
int offset(int yearNumber, int monthNumber, int numberofDaysinaMonth, int years\
);

bool isLeapYear(int yearNumber)
{
if(yearNumber % 4 == 0 && yearNumber % 100 != 0)
return true;
else if(yearNumber % 400 == 0)
return true;
else
return false;
}

int getMonth()
{
int monthNumber;
for (int a=0; a<1;)
{
cout << "Enter a month number: ";
cin >> monthNumber;
if (monthNumber < 0 || monthNumber > 12)
{
cout << "Month must be between 1 and 12.\n";
monthNumber = 0;
continue;
}
a = 1;
}

return monthNumber;

}
int getYear()
{
int yearNumber;
for(int b=0; b<1;)
{
cout << "Enter year: ";
cin >> yearNumber;
if(yearNumber < 1753)
{
cout << "Year must be 1753 or later.\n";
yearNumber = 0;
continue;
}
b = 1;
}
return yearNumber;
}

int offset(int yearNumber, int monthNumber, int numberofDaysinaMonth,
int years)
{
int days = 0;
int offsetNumber;
for(int b = 1753; b < yearNumber; b++)
{
days = days + years;
}
for(int c = 1; c < monthNumber; c++)
{
days = days + numberofDaysinaMonth;
}
offsetNumber = days % 7;
return offsetNumber;
}

int daysMonth(int monthNumber, int yearNumber)
{
int numberofDaysinaMonth;

if(monthNumber = 1)
numberofDaysinaMonth = 31;
else if(monthNumber = 2)
{
if(isLeapYear(yearNumber))
{
numberofDaysinaMonth = 28;
}
else
numberofDaysinaMonth = 29;
}
else if(monthNumber = 3)
numberofDaysinaMonth = 31;
else if(monthNumber = 4)
numberofDaysinaMonth = 30;
else if(monthNumber = 5)
numberofDaysinaMonth = 31;
else if(monthNumber = 6)
numberofDaysinaMonth = 30;
else if(monthNumber = 7)
numberofDaysinaMonth = 31;
else if(monthNumber = 8)
numberofDaysinaMonth = 31;
else if(monthNumber = 9)
numberofDaysinaMonth = 30;
else if(monthNumber = 10)
numberofDaysinaMonth = 31;
else if(monthNumber = 11)
numberofDaysinaMonth = 30;
else if(monthNumber = 12)
numberofDaysinaMonth = 31;
return numberofDaysinaMonth;
}
int daysYear(int yearNumber)
{
int years;
if(isLeapYear(yearNumber))
years = 366;
else
years = 365;
}

void display(int monthNumber,int yearNumber, int offsetNumber, int numberofDays\
inaMonth)
{
if(monthNumber = 1)
cout << "January, " << yearNumber << endl;
else if(monthNumber = 2)
cout << "Febuary, " << yearNumber << endl;
else if(monthNumber = 3)
cout << "March, " << yearNumber << endl;
else if(monthNumber = 4)
cout << "April, " << yearNumber << endl;
else if(monthNumber = 5)
cout << "May, " << yearNumber << endl;
else if(monthNumber = 6)
cout << "June, " << yearNumber << endl;
else if(monthNumber = 7)
cout << "July, " << yearNumber << endl;
else if(monthNumber = 8)
cout << "August, " << yearNumber << endl;
else if(monthNumber = 9)
cout << "September, " << yearNumber << endl;
else if(monthNumber = 10)
cout << "October, " << yearNumber << endl;
else if(monthNumber = 11)
cout << "November, " << yearNumber << endl;
else if(monthNumber = 12)
cout << "December, " << yearNumber << endl;
cout << endl;
cout << " Su Mo Tu We Th Fr Sa\n";
if(offsetNumber == 0)
cout << setw(4) <<" ";
else if(offsetNumber == 1)
cout << setw(8) <<" ";
else if(offsetNumber == 2)
cout << setw(12) <<" ";
else if(offsetNumber == 3)
cout << setw(16) <<" ";
else if(offsetNumber == 4)
cout << setw(20) <<" ";
else if(offsetNumber == 5)
cout << setw(24) <<" ";
else if(offsetNumber == 6)
cout << " ";
for(int d = 1; d <= numberofDaysinaMonth; d++)
{
if((offsetNumber + d) % 7 == 0)
cout << endl;
cout << setw(4) << d;
}


}

int main(int monthNumber, int yearNumber, int offsetNumber, int numberofDaysina\
Month)
{
getMonth();
getYear();
display(monthNumber, yearNumber, offsetNumber, numberofDaysinaMonth);
return 0;
}


It shouldn't even compile, int DaysYear() has no return value. Fix this and it may compile and run. Your code is difficult to read without formatting. Use code tags and whitespace. Below are some general suggestions.

also consider const ints as apposed to magic numbers as in
const int JANUARY = 1;

then use JANUARY whenever 1 means january

lastly, switch statements would make it easier to read, also faster, as in
1
2
3
4
5
6
7
8
9
switch(monthNumber)
{
case JANUARY:
    // do something
    break;
case FEBRUARY:
    //do something
    break;
}


oh and use code tags =)

edit: what IDE are you using, or are you using an IDE
Last edited on
Topic archived. No new replies allowed.