| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
c++ Calendar
Hello all,
Was wondering if u could help me with a c++ calendar here is the code i got so far no i need to add the actual days 1 set varieble numblankdays to 0 2 if numblankdays is equal to startday go to step 6 3 print 6 blanks 4 incement numblankdays 5 go to step 6 set variable day = 6 just brain farted after all that code i did Last edited by warp_9 : November 25th, 2004 at 04:18 PM. Reason: added c++ code |
|
#2
|
|||
|
|||
|
Cal
Here's the code from a C++ assigment i had a few months ago.
PHP Code:
|
|
#3
|
|||
|
|||
|
Thought I'd share mine
I had a very simular project and thought I'd share my code with everyone.
Code:
/************************************************** **************************
Description: This program outputs a calendar for the year that the user inputs.
If no year is given, then it randomly selects one from 1400 and 9999.
************************************************** **************************/
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
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
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 main()
{
year = getYear(); // has user enter year number
printAll(year);
return 0;
}
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;
}
}
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;
} // end BIG for loop
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > c++ Calendar |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|