954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

calendar code firstday problem

My problem is that when i run the program i ask the user to select a month from a menu then input a year. I then calulate the first day and janaury for that year. Then i have to find the first day of the month the user selected but when i print out my calendar the first day dostn match up and is way off. Please help

#include <iostream>				    // Need for cout,cin
#include <iomanip>                   // Need setf,fixed,showpoint,setprecision
#include <stdio.h>				    // Need for getchar
#include <fstream>					// Needed for files
#include <cstdlib>					// Needed for exit function
#include <string>					// Need for string class
#include <cmath>					// Needed for floor function

using namespace std;


int getmonth();
int getyear();
bool leapyear (int year);




int main ()
{

	int month;
	double year;
	int firstday;
	int x;
	int daysinmonth;
	int isleapyear;
	int md;
	int fmonth;
 int days[12]={31,28,31,30,31,30,31,31,31,30,31};

	month = getmonth();

	year = getyear();
	isleapyear = leapyear(year);




	//if ((!year % 4) && ( year % 100)) || !(year % 400)
	//	cout << "leap year";


	// calulates first day

	firstday = ((((year-1)*365)+ floor((year-1)/4)- floor((year-1)/100)+ floor((year-1)/400))+1);
	firstday = firstday%7;





	// first day is 0 for sunday 1 for monday 2 for tuesday etc...


	if ( month == 1 || month == 3 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		md = 31;
	}

	else if (month == 2 && isleapyear) 
	{
		md = 29;
	}
	else if (month == 2 && !isleapyear)
	{
		md = 28;
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		md = 30;
	}
	if ( month == 1)
		cout << "January" << setw(20) << year << endl;
	else if ( month == 2)
		cout << "February" << setw(20) << year << endl;
	else if ( month == 3)
		cout << "March" << setw(20) << year << endl;
	else if ( month == 4)
		cout << "April" << setw(20) << year << endl;
	else if ( month == 5)
		cout << "May" << setw(20) << year << endl;
	else if ( month == 6)
		cout << "June" << setw(20) << year << endl;
	else if ( month == 7)
		cout << "July" << setw(20) << year << endl;
	else if ( month == 8)
		cout << "August" << setw(20) << year << endl;
	else if ( month == 9)
		cout << "September" << setw(20) << year << endl;
	else if ( month == 10)
		cout << "October" << setw(20) << year << endl;
	else if ( month == 11)
		cout << "November" << setw(20) << year << endl;
	else if ( month == 12)
		cout << "December" << setw(20) << year << endl;
	 if (month == 1) 
                fmonth = firstday; 
        else if (month == 2) 
                fmonth = (days[0] % 7) + firstday; 
        else if (month == 3) 
                fmonth =((days[0] + days[1]) % 7) + firstday; 
        else if (month == 4) 
                fmonth =((days[0] + days[1] + days[2]) % 7) + firstday; 
        else if (month == 5) 
                fmonth =((days[0] + days[1] + days [2] + days[3]) % 7) + firstday; 
        else if (month == 6) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4]) % 7) + firstday; 
        else if (month == 7) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5]) % 7) + firstday;  
        else if (month == 8) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6]) % 7) + firstday; 
        else if (month == 9) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7]) % 7) + firstday;  
        else if (month == 10) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8]) % 7) + firstday; 
        else if (month == 11) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8] + days[9]) % 7) + firstday; 
        else if (month == 12) 
                fmonth =((days[0] + days[1] + days [2] + days[3] + days[4] + days[5] + days[6] + days[7] + days[8] + days[9] + days [10]) % 7) + firstday; 

 
        if ((isleapyear == true) && (month != 1)) 
                fmonth = fmonth + 1; 
        if (fmonth >= 7) 
                fmonth -= 7; 




	cout << "-----------------------------" << endl; 
	cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
	int i = 0; 

	for (i = 0; i < fmonth; i++)
	{
		cout << " "; 
	}
		for (i = 1; i <= md; i++) 
		{ 
			cout << setw(4) << i; 
			if ((i + fmonth) % 7 == 0) 
				cout << endl;
		}
	 




	cout.setf (ios::showpoint );
	cout.setf( ios::fixed);
	cout << setprecision(2);

	cout << " Press Enter to continue" << endl;
	cin.ignore();
	char ch = getchar();


	return 0;

}

//****************getmonth***Function***************************
//
// Function name: getmonth
//
// Purpose: to prompt for the month
//              
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: The month selected fromt he list using a number
//               
//
//************************************************************

int getmonth()
{
	int tempmonth = 0;
	{
		cout << "Please select a month from the list" << endl;



		cout << "1. January" << endl; 
		cout << "2. February" << endl;
		cout << "3. March" << endl;
		cout << "4. April" << endl;
		cout << "5. May" << endl;
		cout << "6. June" << endl;
		cout << "7. July" << endl;
		cout << "8. August" << endl;
		cout << "9. September" << endl;
		cout << "10. October" << endl;
		cout << "11. November" << endl;
		cout << "12. December" << endl;
		cin >> tempmonth;
		if (tempmonth > 0 && tempmonth < 13)
		{
			return tempmonth;
		}
		else
		{ 
			cout << "Please enter a selection from 1-12" << endl;
			getmonth();
		}
	}


}

//****************getyear***Function***************************
//
// Function name: getyear
//
// Purpose: to prompt for the year
//              
//
// Input parameters: none
//
// Output parameters: none
//
// Return Value: The year the user inputs
//               
//
//************************************************************

int getyear()
{
	int tempyear;
	{
		cout << " Please enter the year" << endl;
		cin >> tempyear;
	}
	return tempyear;

}



//****************leapyear***Function***************************
//
// Function name: leapyear
//
// Purpose: To determine fi the year the user inputted is a leap year or not
//              
//
// Input parameters: int year
//
// Output parameters: none
//
// Return Value: true or false
//               
//
//************************************************************
bool leapyear(int year)

{
	if ((!(year%4)&&(year%100))||!(year%400))

		return true;

	else 

		return false;

}
smeghead007
Light Poster
40 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Edit:
...it does seem to work when I compile your code, as is. The first line is off, but that's only the formatting (needs to go a bit to the right).

However, I do see this:

int isleapyear;

else if (month == 2 && isleapyear)


You can't compare an int value in your if statement. You could compare it if it was a bool though.

MyrtleTurtle
Junior Poster
101 posts since Feb 2010
Reputation Points: 63
Solved Threads: 11
 
You can't compare an int value in your if statement. You could compare it if it was a bool though.

Yes you can. You can use any numeric value/type in an if statement. The value zero (0) is interpreted as false and any non-zero value is interpreted as true. This is C++, but if it were C it would be your only option because the bool type doesn't exist in C.

The variable isleapyear probably should be a bool to minimize the likelihood of there being issues with the return from leapyear() (there shouldn't be any). As written though, it's perfectly acceptable.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 
Yes you can.

Oops. Sorry. Thanks for the correction. :$

MyrtleTurtle
Junior Poster
101 posts since Feb 2010
Reputation Points: 63
Solved Threads: 11
 
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};


Try with the above modified days array.

thomas_naveen
Junior Poster
168 posts since Jan 2010
Reputation Points: 136
Solved Threads: 36
 

Thanks alot guys for the help as for the days array its only 11 because december you dont need the formula finds the 1st day of January but thanks again guys I owe you

smeghead007
Light Poster
40 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

take a look again.... even if you do only need 11 elements, 3 of the 11 that you have are not correct

//your array:
int days[12]={31,28,31,30,31,30,31,31,31,30,31};
//your array has Jul, Aug, and Sep. all w/ 31 days, which is not correct
//Sep. only has 30 days, as a result, Oct, Nov, and Dec are wrong as well
//especially when you consider that Dec is indicated as 0 days

//thomas_naveen's array:
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//this is the correct day distribution


Take time to think about and analyze peoples' responses before you dismiss them as irrelevant.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

sorry didnt notice that i corrected it thank you

smeghead007
Light Poster
40 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You