calendar calculator help

Started by
10 comments, last by z80 17 years, 3 months ago
hi. I'm trying to create a calendar calculator where you give a year and a month anytime after 1 jan 1800 and you get a calendar with the 1st of the month under the right day. so far i have created a leap year converter which works. i now need to use the code i have to count the amount of days from 1st jan 1800, to the date i choose. can anyone help me out. here is my code: code: #include<iostream> // cin, cout, <<, >>, endl; using namespace std; //-------------------------------------------------------- bool aLeapYear (int theyear); int theYear; int daysInTheMonth(int theMonth, int theYear); int theMonth; int daysSince1800 (int theMonth, int theYear); //-------------------------------------------------------- int main() { for(;;) { int theYear; int theMonth; cout << "\nEnter the year --> "; cin >> theYear; cout << "\nEnter Month --> "; cin >> theMonth; cout << daysInTheMonth(theMonth, theYear) << "\n\n\n"; } } //-------------------------------------------------------- bool aLeapYear(int theYear) { if ((0 == theYear%400) || (0 == theYear%4 && theYear%100)) { return true; } else { return false; } } //-------------------------------------------------------- //-------------------------------------------------------- int daysInTheMonth(int theMonth, int theYear) { if (theMonth == 1 || theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 || theMonth == 8 || theMonth == 10|| theMonth == 12) { return 31; } else if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) { return 30; } else if (theMonth == 2) { if (aLeapYear(theYear)) { return 29; } else { return 28; } } else { return 0; } } //-------------------------------------------------------- //-------------------------------------------------------- int daysSince1800 (int theMonth, int theYear, );
Advertisement
Alright, first tip.

Please post your code in source blocks ([source][/source]) to make things much easier to read.

The second thing is, figure out what algorithm you need to make such a calculation, and implement it. Outside of that, what exactly did you need help with?
Not all that important, but

bool aLeapYear(int theYear){   return (0 == theYear%400) || (0 == theYear%4 && theYear%100);}


is a lot cleaner than

bool aLeapYear(int theYear){if ((0 == theYear%400) || (0 == theYear%4 && theYear%100)){return true;}else {return false;}
Quote:
if ((0 == theYear%400) || (0 == theYear%4 && theYear%100))


should be:

if ( 0 == theYear%4 )


Because "theYear%400" will be true if the other two are correct
and a leap year is every 4 years not every 100.
http://www.gamedev.net/community/forums/post.asp?method=reply&topic_id=430966&Quote=2
Reply to Topic - GameDev.Net Discussion Forums
As a side note, the Year%100 will return false if it is divisble by 0 because you do not have an "==" operator.

//--------------------------------------------------------	int daysInTheMonth(int theMonth, int theYear){	if      (theMonth == 1 || theMonth == 1 || theMonth == 3 || 			 theMonth == 5 || theMonth == 7 || theMonth == 8 || 			 theMonth == 10|| theMonth == 12)	{		return 31;	}			else if	(theMonth == 4 || theMonth == 6 || theMonth == 9 ||	   		 theMonth == 11)	{		return 30;	}			else if	(theMonth == 2)	{		if (aLeapYear(theYear))		{			return 29;		}				else 		{			return 28;		}					}			else 	{		return 0;	}		}//--------------------------------------------------------


should be:

//--------------------------------------------------------	int daysInTheMonth(int theMonth, int theYear){	if      (theMonth == 1 || theMonth == 3 ||  // 2 theMonth==1 is repetative			 theMonth == 5 || theMonth == 7 || theMonth == 8 || 			 theMonth == 10|| theMonth == 12)	{		return 31;	}	else if	(theMonth == 4 || theMonth == 6 || theMonth == 9 ||	   		 theMonth == 11)	{		return 30;	}	else if	(theMonth == 2)	{		if (aLeapYear(theYear))		{			return 29;		}		else 		{			return 28;		}	}	else 	{		return 0;	}}//--------------------------------------------------------


as a side note if you get returned a 0 you should return a error message.
slymrHopefully game is in progress.
Quote:Original post by slymr
Quote:
if ((0 == theYear%400) || (0 == theYear%4 && theYear%100))


should be:

if ( 0 == theYear%4 )


Because "theYear%400" will be true if the other two are correct
and a leap year is every 4 years not every 100.
http://www.gamedev.net/community/forums/post.asp?method=reply&topic_id=430966&Quote=2
Reply to Topic - GameDev.Net Discussion Forums
As a side note, the Year%100 will return false if it is divisble by 0 because you do not have an "==" operator.


No.

In the Gregorian calendar there is a leap year every four years, apart from every 100 years when there isn't one, with the exception of every 400 years when there is. Therefore that particular piece of code is actually correct, and having Year%100 evalute to 0 is desired behaviour in this case.
Not to sound harsh but this is your third thread on the matter and you don't seem to be getting very far.
Before I help is this homework?
sorry about the amount of posts. No this isn't homework. Ive been trying to teach myself c++ for about 3 months. i saw a calendar calculator written in c but it was really badly coded. worked though. so i thought i'd set myself the challange of doing it in c++. i just didn't know it would be so hard.

About the leap year function i only learned lately through research that a leap year only happens at the beginning of a new century every 400 years.

where i need help is basically i dont know where to go from here. i thought my next step would be to count days. then i could say if 8%7 would give me 1 and that would help me to format my calendar but i dont know how i'm going to add the days between 1st jan 1800 (chosen randomly change if you wish) and the day that is typed in, in the main.

ty for the repatition thing. i missed that
heh, this is kind of interesting, I'm developing a calendar program for myself to use as a planner type of thing for my homework and stuff, anyways, I found it was easier to simply calculate the day of the week of a month of a year and go from their . . .

If you google "Day of the week algorithm" or something similar, you can find some pretty good ones, but you'll have to adapt them yourself to work . . For example, I only made mine functional from 2000-2099 as I probably wont need it by 2100 . . . but it sounds like you need a wider range, so you'll just have to modify the algorithms a little
Ok, well there are numerous ways of doing this.
1)You have a start date 1/1/1800 which I think was a Wednesday.
if a year is not a leap year then the next 1 st Jan will be the next day in the week, if it was a leap year then it will be two days.
example
1st Jan 1800 wednesday
1st Jan 1801 thursday
1st Jan 1802 friday
1st Jan 1803 saturday
1st Jan 1804 sunday
1st Jan 1805 tuesday//the year before 1804 was a leap year.

So this can give you the day which is the start of the year from this you can work out how many days are in the months before the month selected.

2) You could work all this information out before hand and store it in a file, ask the user for the year and then read in what day Jan 1st was. But seeing as your new to C++ I would suggest going with 1.

Does that give you a starting point or do you just want the code?

[edit]Good thinking samoz I didn't even think of searching for an algorithm, heres some interesting reading for you.
thanks. No i dont want the exact code, just a starting point. thanks for the help all

This topic is closed to new replies.

Advertisement