#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <ctime>
using namespace std;
int getYear(); // has the user enter a valid year
bool isLeap(int year); // check for leap years
void dayName(); // prints the names for the day of the week
void monthNameHeader(int year); // puts head for the month name
int startDay(int year); // decides what week day Jan starts on
int monthCount(int counter); // how many days are in each month
void newMonth(int startDOW); // what day of the week new month starts on
void printAll(int year);// puts everything together, prints to screen
void calen_call();
void display();
int year = 0; // uesr inputed year || rand gen per 0
int counter = 1; // counter for month name & # days in month
int startDOW, // day of the week Jan starts on
wrap, // check for if weekday is Saturday
daysInMonth; // total days in each month
int weekNumber = 0; // flag for first week of the month
int days; /* Will have no. of days a month has */
int mm; /* Month Iterator variable [1-12] */
int k; /* Days Iterator [ 0 till the days in month ] */
int z; /* Iterator Variable */
int p; /* Start day of every month e.g. 5 for friday */
int yy; /* User Entered year */
int r; /* Year Iterator variable */
int calen[5][7]; /* The Matrix stores all required dates */
int i, j; /* Iterator Variables */
int mon;
int main()
{
//year = getYear(); // has user enter year number
calen_call();
display();
//getch();
//printAll(year);
switch (mon)
{
case 1:cout << "\n\n\t\t\t\tJANUARY\n\n"; break;
case 2:cout << "\n\n\t\t\t\tFEBRUARY\n\n"; break;
case 3:cout << "\n\n\t\t\t\tMARCH\n\n"; break;
case 4:cout << "\n\n\t\t\t\tAPRIL\n\n"; break;
case 10:cout << "\n\n\t\t\t\tOCTOBER\n\n"; break;
case 11:cout << "\n\n\t\t\t\tNOVEMBER\n\n"; break;
case 5:cout << "\n\n\t\t\t\tMAY\n\n"; break;
case 6:cout << "\n\n\t\t\t\tJUNE\n\n"; break;
case 7:cout << "\n\n\t\t\t\tJULY\n\n"; break;
case 8:cout << "\n\n\t\t\t\tAUGUST\n\n"; break;
case 9:cout << "\n\n\t\t\t\tSEPTEMBER\n\n"; break;
case 12:cout << "\n\n\t\t\t\tDECEMBER\n\n"; break;
default:cout << "\n\nWRONG ENTRY"; exit(0);
}
/* Start day of year 1999 i.e. Friday 1st January 1999 */
p = 5;
/* Determination of start day of entered year [Calculating from 1999] */
for (r = 1999; r<yy; r++)
{
if (r % 4 == 0)
{
if (r % 100 == 0)
{
if (r % 400 == 0)
p = p + 2;
else
p++;
}
else
p = p + 2;
}
else
p++;
/* Resseting of start variable if last day is reached */
if (p == 7)
p = 0;
if (p == 8)
p = 1;
}
/* Start of Month iteration for entered year */
for (mm = 0; mm<12; mm++)
{
/* Number of days a month has. Takes from the array defined above */
days = month[mm];
/* Start of days from */
k = 0;
/* Setting all values in first row to 0 */
for (z = 0; z<7; z++)
calen[0][z] = 0;
/* Looping through the rows and columns of calen */
for (i = 0; i<5; i++, p = 0)
for (j = p; j<7 && k<days; j++)
{
/* Adding of days to the matrix */
calen[i][j] = k + 1;
k++;
/* Checking for finish of days */
if (k == days&&i == 4 && j<7)
{
/* Setting of remaining values to 0 */
for (p = j + 1; p<7; p++)
calen[i][p] = 0;
}
/* Putting values on first row if matrix is finished */
if (k<days&&i == 4 && j == 6)
{
for (p = 0; p<7 && k<days; p++, k++)
calen[0][p] = k + 1;
j = p - 1;
}
/* Setting of j to -1 when days are finished */
if (k == days&&i == 4 && j == 6)
{
j = -1;
}
/* Special Check if days finish in 4th row e.g. 2009 february */
if (k == days&&i == 3 && j == 6)
{
/* Setting of final row to 0 */
for (p = 0; p<7; p++)
calen[4][p] = 0;
}
}
/* Resetting of start day for next month */
p = j;
/* Returning of loop if desired month is found */
if (mm + 1 == mon)
return;
}
}
void display()
{
/* Printing of day names */
cout << "\n\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n";
/* Start of loop of rows */
for (i = 0; i<5; i++)
{
/* Start of loop of columns */
for (j = 0; j<7; j++)
cout << "\t" << calen[i][j];
cout << "\n\n";
}
//getch();
}
int getYear() //prompts the user to enter a valid year
{
char c;
srand(time(NULL));
cout << "Enter the year, or 0 for and random year: ";
do { // gets whole number value
cin.get(c);
if (isdigit(c))
{
year = year * 10;
year += (int)(c - '0');
}
} while (c != '\n');
if (year == 0) // if no response or 0 are enter, random a year
{
year = rand() % 8600 + 1400;
cout << "\nThe random year " << year << " will be evaluated\n\n";
}
return year;
}
bool isLeap(int year) // checking for possible leap year
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false; // else return false
}
void dayName()
{
cout << " S M T W T F S" << endl;
cout << "---------------------" << endl;
}
void monthNameHeader(int year)
{
switch (counter)
{
case 1:
cout << " January " << year << endl;
break;
case 2:
cout << " February " << year << endl;
break;
case 3:
cout << " March" << year << endl;
break;
case 4:
cout << " April " << year << endl;
break;
case 5:
cout << " May " << year << endl;
break;
case 6:
cout << " June " << year << endl;
break;
case 7:
cout << " July " << year << endl;
break;
case 8:
cout << " August " << year << endl;
break;
case 9:
cout << " September " << year << endl;
break;
case 10:
cout << " October " << year << endl;
break;
case 11:
cout << " November " << year << endl;
break;
case 12:
cout << " December " << year << endl;
break;
}
}
int monthCount(int counter) // how many days are in the month
{
switch (counter)
{
case 1:
daysInMonth = 31; // current month days
break;
case 2: // checks for possible leap year
if (isLeap(year))
daysInMonth = 29;
if (!isLeap(year))
daysInMonth = 28;
break;
case 3:
daysInMonth = 31;
break;
case 4:
daysInMonth = 30;
break;
case 5:
daysInMonth = 31;
break;
case 6:
daysInMonth = 30;
break;
case 7:
daysInMonth = 31;
break;
case 8:
daysInMonth = 31;
break;
case 9:
daysInMonth = 30;
break;
case 10:
daysInMonth = 31;
break;
case 11:
daysInMonth = 30;
break;
case 12:
daysInMonth = 31;
break;
}
return 0;
}
int startDay(int year)
{
startDOW = (year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;
return startDOW; // formula for what DoWeek year starts on
}
void printAll(int year)
{
for (counter = 1; counter <= 12; counter++)
{
monthNameHeader(year); // prints month day
dayName(); // prints the name of days
if (counter == 1)
wrap = startDay(year); // what day Jan starts on
else
startDOW = wrap; // what day other months start on
cout << " ";
for (int loopCount = 0; loopCount < startDOW; loopCount++)
{
cout << " "; // how many space to indent new month
}
monthCount(counter); // how many days in month
for (int dayCounter = 1; dayCounter <= daysInMonth; dayCounter++)
{
if (wrap == 7) //if Saturday, carriage return
{
cout << "\n ";
wrap = 0; //resets day of week counter
weekNumber++; //no longer first week of month
}
if (dayCounter<10) //adds space for single digit days
cout << " ";
cout << dayCounter << " "; //prints the day #
wrap++;
}
//cout << "\nthis month starts on day number " << startDOW; // *testing*
//cout << "\ndays in this month are " << daysInMonth; //*testing*
cout << "\n\n";
system("PAUSE");
cout << endl;
}
}
ctime would normally be the name of a header file.
i notice you have both
1 2
#include <time.h>
#include <ctime>
You don't need both of these, <time.h> is part of the C language, while <ctime> is the C++ named for the same header.
I suggest you change the headets to all use the C++ versions:
Note, it's much more concise to look up the name in a table like this, rather than use a long switch-case statement. The function month_name() is also re-usable if needed in more than one place in the program.
There is no such variable in your code named ctimes.
Of course the compiler is going to say "ctimes not declared in scope" if you try and reference it and it is not defined.
If I comment out that line, your program does run.
CALENDAR FROM YEAR 2000...
ENTER YEAR [2000 ...] : 2014
ENTER MONTH [1-12] :6
YEAR 2014
JUNE
SUN MON TUE WED THU FRI SAT
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 0 0 0 0 0
Some comments on your code:
1) Use of globals should be avoided.
2) Use of goto should be avoided.
3) You output month names is several different places. This should be done in a common function.
4) You calculate days per month in multiple places. This should be done once. In one place, it's an array. In another it's a switch statement. Confusing and repetitive.
5) Line 204: srand() should be called once and only once at the beginning of your program. Calling srand() inside a function will reset the random number generation each time you call that function.
6) Your code outputs zeroes for unused cells. These should be suppressed.
7) Your unformatted code is hard to read.
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.