Important alert: (current site time 6/19/2013 7:38:08 PM EDT)
 

VB icon

calender funtion-beginner

Email
Submitted on: 7/13/2012 5:39:21 PM
By: pimpsn00ky 
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general)
Views: 1220
 
     generates a calender given the input year, day of the week the year starts, month to be displayed
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: calender funtion-beginner
// Description:generates a calender given the input year, day of the week the year starts, month to be displayed
// By: pimpsn00ky
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13801&lngWId=3//for details.//**************************************

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include<conio.h>
using namespace std;
int daysPerMonth(int month,int year);
string getMonth(int month);
int calcPush(int initDay, int dispMonth,int year);
int year, day, month,feb, errCount = 0;
void main()
{
	// enter year,day and month. allows 3 errors each input or Todor boots you
	cout<< "Enter the year: ";
	cin >> year;
	while (!cin.good())
	{
		while ( errCount <3 )
		{
			cout<<"Todor: \"Too many attempts, Goodbye\"\a"<<endl;
			exit(1);
		}
		cout << "Enter A Valid Year\n";
		cin.clear();
		cin.ignore(80,'\n');
		errCount++;
		cin >> year;
		
	}
	cout << "Enter the day of the week the years starts" <<endl;
	cout << "Sun=1 Mon=2 Tue=3 Wed=4 Thu=5 Fri=6 Sat=7"<<endl;
	cin >> day;
	errCount=0;
	while(!cin.good() || day < 1 || day > 7)
	{
		while (errCount <3 )
		{
			cout<<"Todor: \"Too many attempts, Goodbye\"\a"<<endl;
			exit(1);
		}
		cout << "Enter a number 1-7 for the day" << endl;
		cin.clear();
		cin.ignore(80, '\n');
		errCount++;
		cin >> day;
		cout <<"\n\n\n"<<endl;
	}
	cout << "Enter the month to display (1-12)" << endl;
	cout << "Jan=1 Feb=2 Mar=3 Apr=4 May=5 Jun=6"<< endl;
	cout << "Jul=7 Aug=8 Sep=9 Oct=10 Nov=11 Dec=12"<< endl;
	cin >> month;
	errCount =0;
	while(!cin.good() || month < 1 || month > 12)
	{
		while (errCount <3 )
		{
			cout<<"Todor: \"Too many attempts, Goodbye\"\a"<<endl;
			exit(1);
		}
		cout << "Please enter a number 1-12" << endl;
		cin.clear();
		cin.ignore(80, '\n');
		errCount++;
		cin >> month;
		cout <<"\n\n\n"<<endl;
	}
	
	int dayLimiter = daysPerMonth( month,year);
	string calenderHeading = getMonth(month);
	cout <<"\n\n\n"<<endl;
	//universal heading pattern
	cout << setw(15) << calenderHeading <<" "<< year << "\n"<< endl;
	cout <<"______________________________________"<<endl;
	cout <<" Sun Mon Tues Wed Thur Fri Sat" << endl;
	cout <<"______________________________________"<<endl;
	
	// fills prints the days of the week after figuring out where to start
	int startDay = (calcPush(day,month,year))%7;
	int counter = startDay;
		cout<<setw(startDay*5)<<"1";
			
	for(int i = 2; i <= dayLimiter; i++)
	{
		
		cout << setw(5) << i;
		counter++;
		if(counter == 7)
		{
			cout<<"\n";
			counter=0;
		}
		
	}
	cout<<"\n\n"<<endl;
	cin >> counter;
}
// switches number entered to a string display for calander heading
string getMonth(int month)
{
	string header;
	switch (month)
	{
		case 1:
			header = "January";
			break;
		case 2:
			header = "Febuary";
			break;
		case 3: 
			header = "March";
			break;
		case 4:
			header = "April";
			break;
		case 5:
			header = "May";
			break;
		case 6:
			header = "June";
			break;
		case 7:
			header = "July";
			break;
		case 8:
			header = "August";
			break;
		case 9:
			header = "September";
			break;
		case 10:
			header = "October";
			break;
		case 11:
			header = "November";
			break;
		case 12:
			header = "December";
			break;
		default:
			header= "Invalid";
	}
	return header;
}
// sets number of days per month for counter
int daysPerMonth(int month,int year)
{
 int numberDays=0;
 if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
 { numberDays=31;}
 else if(month==4 || month==6 || month==9 || month==11)
 {numberDays=30;}
 else if((year%100!=0 && year%4==0) || year%400==0)
 { numberDays=29;}
 else
		 numberDays=28;
 return numberDays;
}
		
// based on starting day pushes out calender the 
//appropriate number of days and checks for leap year
int calcPush(int initDay, int dispMonth,int year)
{
	int output;
	
	feb=28;
	if((year%100!=0 && year%4==0) || year%400==0)
		feb=29;
	
	switch(dispMonth)
	{
	case 1:
		output = initDay;
		break;
	case 2:
		output = 31;
		break;
	case 3:
		output = 31+feb;
		break;
	case 4:
		output = 62+feb;
		break;
	case 5:
		output = 92+feb;
		break;
	case 6:
		output = 123+feb;
		break;
	case 7:
		output = 153+feb;
		break;
	case 8:
		output = 184+feb;
		break;
	case 9:
		output = 215+feb;
		break;
	case 10:
		output = 245+feb;
		break;
	case 11:
		output = 276+feb;
		break;
	case 12:
		output = 306+feb;
		break;
	default:
		output = 0;
		break;
	}
	return output;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.