Jump to content
  • Advertisement
Sign in to follow this  
sjaakiejj

Coding a Calender, problem (c++)

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm coding a calender in C++ for some excercise at school, however everytime when I test the program, it gives me the correct datum + day in february 2008, but only in february 2008. When march starts, the calender suddenly skips 2 days and goes from friday to monday :S Here is my code, if I wasn't 100% clear on what I meant, try running it yourself. Skriv in Datum = Enter a date År = Year Månad = Month Dag = Day Try for example Year: 2008 Month: 3 Day: 20 and Year: 2008 Month: 2 Day: 20
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void datuminmatning (int &ar, int &man, int &dag);
void skrivmanad(int nr);
void skrivdatum(int &ar, int &manad, int &dag);
void skrivveckodag (int dagnummer);
int antdagar(int ar, int man, int dag);
int antdagarimanad(int ar, int man);
void almanacka(int &ar, int &manad);

void main()
{
	int ar=0, man=0, dag=0, manad=0;
	while (true)
	{
		datuminmatning(ar, man, dag);
		if (ar<1900)
			break;
		else if (man>12)
			cout << "ogiltig" << endl;
		else{
			skrivdatum(ar, man, dag);
			almanacka(ar, man);}
	}
}

void datuminmatning(int &ar, int &man, int &dag)
{
	cout << "skriv in en datum" << endl << "Året: ";
	cin >> ar;
	cout << "Månaden: ";
	cin >> man;
	cout << "Dagen: ";
	cin >> dag;
}

void skrivmanad(int nr)
{
	switch(nr)
	{
	case 1: cout << "Januari"; return;
	case 2: cout << "Februari"; return;
	case 3: cout << "Mars"; return;
	case 4: cout << "April"; return;
	case 5: cout << "Maj"; return;
	case 6: cout << "Juni"; return;
	case 7: cout << "Juli"; return;
	case 8: cout << "Augustus"; return;
	case 9: cout << "September"; return;
	case 10: cout << "Oktober"; return;
	case 11: cout << "November"; return;
	case 12: cout << "December"; return;
	default: cout << "ogiltig"; return;
	}
}

void skrivdatum(int &ar, int &manad, int &dag)
{

	cout << "Datum är: ";
	skrivveckodag(antdagar(ar, manad, dag)%7);
	cout << " " << dag << " ";
	skrivmanad(manad);
	cout << " " << ar << endl;
}

void skrivveckodag(int dagnummer)
{
	switch(dagnummer)
	{
	case 0: cout << "Söndag"; return;
	case 1: cout << "Måndag"; return;
	case 2: cout << "Tisdag"; return;
	case 3: cout << "Onsdag"; return;
	case 4: cout << "Torsdag"; return;
	case 5: cout << "Fredag"; return;
	case 6: cout << "Lördag"; return;
	default: cout << "ogiltig"; return;
	}
}

int antdagar(int ar, int man, int dag)
{
	int dagnummer=0;
	
	for (int nr=1900; nr<ar-1; ++nr)
	{
		dagnummer=dagnummer+365;
		if (ar%4==0)
			dagnummer=dagnummer+1;
	}
	for (int nr=1; nr<man-1; ++nr)
	{
		dagnummer=dagnummer+antdagarimanad(ar, man);
	}
	dagnummer=dagnummer+dag;
	return dagnummer;
}

int antdagarimanad(int ar, int man)
{
	int dagar=0;
	int yr=0;
	if (ar%100)
	{
		if (ar%400)
			yr=0;
		else
			yr=1;
	}
	else
		yr=1;
	if (yr==0){
		switch (man)
	{	
	case 1: dagar=31; return 31;
	case 2: dagar=29; return 29;
	case 3: dagar=31; return 31;
	case 4: dagar=30; return 30;
	case 5: dagar=31; return 31;
	case 6: dagar=30; return 30;
	case 7: dagar=31; return 31;
	case 8: dagar=31; return 31;
	case 9: dagar=30; return 30;
	case 10: dagar=31; return 31;
	case 11: dagar=30; return 30;
	case 12: dagar=31; return 31;
	default: cout << "ogiltig"; return 0;
		}}
	else{
		switch (man)
	{	
	case 1: dagar=31; return 31;
	case 2: dagar=28; return 28;
	case 3: dagar=31; return 31;
	case 4: dagar=30; return 30;
	case 5: dagar=31; return 31;
	case 6: dagar=30; return 30;
	case 7: dagar=31; return 31;
	case 8: dagar=31; return 31;
	case 9: dagar=30; return 30;
	case 10: dagar=31; return 31;
	case 11: dagar=30; return 30;
	case 12: dagar=31; return 31;
	default: cout << "ogiltig"; return 0;
		}}
}

void almanacka(int &ar, int &manad)
{
	int dagen=0;
	for(float nr=0;++nr<=1;)
	{
		cout << setw(8) << "sön" << setw(8) << "mån" << setw(8) << "tis" << setw(8) << "ons" << setw(8) << "tor" << setw(8) << "fre" << setw(8) << "lör";
		cout << endl;
		}
	for(float nr=0;++nr<=1;)
	{
		
		for (float i=-1;++i<=6;){
			if (i==antdagar(ar, manad, 1)%7){
				dagen=1;
			}
			else if (i>antdagar(ar, manad, 1)%7)
				dagen=dagen+1;
			if (dagen==0){
				cout << setw(8) << "";}
			else
				cout << setw(8) << dagen;
		}
		cout << endl;
		}
	for(float nr=0;++nr<=5;)
	{
	
		for (float i=-1;++i<=6;){
			dagen=dagen+1;
			cout << setw(8) << dagen;
			if (dagen==antdagarimanad(ar, manad))
				break;
		}
		cout << endl;
		if (dagen==antdagarimanad(ar, manad))
			break;
		}
}

ps. How do i use the code tags on this forum? Tried [ code ] but it didn't work :S [Edited by - sjaakiejj on April 4, 2008 3:11:58 AM]

Share this post


Link to post
Share on other sites
Advertisement
First a small tip: Don't use swedish names in your code like that, make it a habit to use english everywhere since all companies in sweden that I've worked for uses english as the default language for code and documentation.

For code tags, click on the faq found in the top right of this page

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void datuminmatning (int &ar, int &man, int &dag);
//Reads the date you enter and sends it to the next functions
void skrivmanad(int nr);
//Writes out the month (ex. January, February etc.)
void skrivdatum(int &ar, int &manad, int &dag);
//Writes out the date
void skrivveckodag (int dagnummer);
//Writes out the weeks day (ex. Monday, Tuesday)
int antdagar(int ar, int man, int dag);
//Counts the number of days starting from 1900
int antdagarimanad(int ar, int man);
//Counts the number of days in the specific month, loop years included.
void almanacka(int &ar, int &manad);
//Writes out the calender

void main()
{
//ar = Year, man = Month, dag= Day, Manad = Month2
int ar=0, man=0, dag=0, manad=0;
while (true)
{
datuminmatning(ar, man, dag);
if (ar<1900)
break;
else if (man>12)
cout << "ogiltig" << endl;
else{
skrivdatum(ar, man, dag);
almanacka(ar, man);}
}
}

void datuminmatning(int &ar, int &man, int &dag)
{
cout << "skriv in en datum" << endl << "Året: ";
//Asks you to enter a date, starting with Year
cin >> ar;
cout << "Månaden: ";
//Month
cin >> man;
cout << "Dagen: ";
//Day
cin >> dag;
}

void skrivmanad(int nr)
{
switch(nr)
{
case 1: cout << "Januari"; return;
case 2: cout << "Februari"; return;
case 3: cout << "Mars"; return;
case 4: cout << "April"; return;
case 5: cout << "Maj"; return;
case 6: cout << "Juni"; return;
case 7: cout << "Juli"; return;
case 8: cout << "Augustus"; return;
case 9: cout << "September"; return;
case 10: cout << "Oktober"; return;
case 11: cout << "November"; return;
case 12: cout << "December"; return;
default: cout << "ogiltig"; return;
}
//Speaks for it's own, January - December
}

void skrivdatum(int &ar, int &manad, int &dag)
{

cout << "Datum är: ";
skrivveckodag(antdagar(ar, manad, dag)%7);
cout << " " << dag << " ";
skrivmanad(manad);
cout << " " << ar << endl;
}

void skrivveckodag(int dagnummer)
{
switch(dagnummer)
{
case 0: cout << "Söndag"; return;
case 1: cout << "Måndag"; return;
case 2: cout << "Tisdag"; return;
case 3: cout << "Onsdag"; return;
case 4: cout << "Torsdag"; return;
case 5: cout << "Fredag"; return;
case 6: cout << "Lördag"; return;
default: cout << "ogiltig"; return;
}
//Sunday - Saterday, Ogiltig = invalid
}

int antdagar(int ar, int man, int dag)
{
int dagnummer=0;

for (int nr=1900; nr<ar-1; ++nr)
{
dagnummer=dagnummer+365;
if (ar%4==0)
dagnummer=dagnummer+1;
}
for (int nr=1; nr<man-1; ++nr)
{
dagnummer=dagnummer+antdagarimanad(ar, man);
}
dagnummer=dagnummer+dag;
return dagnummer;
//Dagnummer= Daynumber
}

int antdagarimanad(int ar, int man)
{
int dagar=0;
int yr=ar%4;
if (yr==0){
switch (man)
{
case 1: dagar=31; return 31;
case 2: dagar=28; return 29;
case 3: dagar=31; return 31;
case 4: dagar=30; return 30;
case 5: dagar=31; return 31;
case 6: dagar=30; return 30;
case 7: dagar=31; return 31;
case 8: dagar=31; return 31;
case 9: dagar=30; return 30;
case 10: dagar=31; return 31;
case 11: dagar=30; return 30;
case 12: dagar=31; return 31;
default: cout << "ogiltig"; return 0;
}}
else{
switch (man)
{
case 1: dagar=31; return 31;
case 2: dagar=29; return 28;
case 3: dagar=31; return 31;
case 4: dagar=30; return 30;
case 5: dagar=31; return 31;
case 6: dagar=30; return 30;
case 7: dagar=31; return 31;
case 8: dagar=31; return 31;
case 9: dagar=30; return 30;
case 10: dagar=31; return 31;
case 11: dagar=30; return 30;
case 12: dagar=31; return 31;
default: cout << "ogiltig"; return 0;
}}
//dagar = Days
}

void almanacka(int &ar, int &manad)
{
int dagen=0;
for(float nr=0;++nr<=1;)
{
cout << setw(8) << "sön" << setw(8) << "mån" << setw(8) << "tis" << setw(8) << "ons" << setw(8) << "tor" << setw(8) << "fre" << setw(8) << "lör";
cout << endl;
}
for(float nr=0;++nr<=1;)
{

for (float i=-1;++i<=6;){
if (i==antdagar(ar, manad, 1)%7){
dagen=1;
}
else if (i>antdagar(ar, manad, 1)%7)
dagen=dagen+1;
if (dagen==0){
cout << setw(8) << "";}
else
cout << setw(8) << dagen;
}
cout << endl;
}
for(float nr=0;++nr<=5;)
{

for (float i=-1;++i<=6;){
dagen=dagen+1;
cout << setw(8) << dagen;
if (dagen==antdagarimanad(ar, manad))
break;
}
cout << endl;
if (dagen==antdagarimanad(ar, manad))
break;
}
}



I haven't looked through the complete code but one thing that I think is missing is that it's not true that every 4:th year have an extra day.
The rule is every fourth years EXCEPT every hundred years EXCEPT evey 400 hundred years.

The function I use:

bool
hasExtraDay(const unsigned int year)
{
if ((year & 3) != 0)
return false;
if ((year % 400) == 0)
return true;
if ((year % 100) == 0)
return false;
return true;
}

Share this post


Link to post
Share on other sites
Thanks for that one :) Still, the issue that I saw is there :S Keeps on returning wrong data.. Skipping days and starting at 2-3 days after the month ended with the new month.

Share this post


Link to post
Share on other sites

for (int nr=1; nr<man-1; ++nr)
{
dagnummer=dagnummer+antdagarimanad(ar, man);
}




I believe that the error is in this loop, but I am not sure how to fix it..

Share this post


Link to post
Share on other sites
entirely recoded the thing, this time in english.. Still havent located the error.

#include <iostream>
#include <iomanip>
using namespace std;

void readdatum(int &year, int &mon, int &day);
void printdatum(int &year, int &month, int &day);
void printmonth(int &nr);
void writeweeksday(int &daynumber);
int numberdays(int year, int month, int day);
int numberdaysmonth(int year, int month);
int dayweek(int year, int month, int day);
void calender(int &year, int &month);

int main()
{
int year=0, mon=0, /*month=0,*/ day=0;
while(true)
{
readdatum(year, mon, day);
if(year<1900)
break;
printdatum(year, mon, day);
calender(year, mon);
}
}

void readdatum(int &year, int &mon, int &day)
{
cout << "Enter a date \nDay:";
cin >> day;
cout << "Month:";
cin >> mon;
cout << "And last but not least, Year: ";
cin >> year;
}

void printmonth(int &nr)
{
switch(nr)
{
case 1: cout << "January"; return;
case 2: cout << "February"; return;
case 3: cout << "March"; return;
case 4: cout << "April"; return;
case 5: cout << "May"; return;
case 6: cout << "June"; return;
case 7: cout << "July"; return;
case 8: cout << "August"; return;
case 9: cout << "September"; return;
case 10: cout << "October"; return;
case 11: cout << "November"; return;
case 12: cout << "December"; return;
default: cout << "ogiltig";
}
}

void printdatum(int &year, int &month, int &day)
{
cout << "The date you entered is: ";
dayweek(year, month, day);
cout << " " << day << " ";
printmonth(month);
cout << " " << year << endl;
}

void writeweeksday(int &daynumber)
{
switch(daynumber)
{
case 0: cout << "Sunday"; return;
case 1: cout << "Monday"; return;
case 2: cout << "Tuesday"; return;
case 3: cout << "Wednesday"; return;
case 4: cout << "Thursday"; return;
case 5: cout << "Friday"; return;
case 6: cout << "Saterday"; return;
default: cout << "Incorrect Data"; return;
}
}

int numberdays(int year, int month, int day)
{
int daynumber=0;
for (int nr=1900; nr<year-1; ++nr)
{
daynumber=daynumber+365;
int loop=0;
if (year%4)
{
if (year%100)
{
if (year%400)
loop=0;
else
loop=1;
}
else
loop=0;
}
else
loop=1;
if (loop==0)
daynumber=daynumber+1;
}

for (int nr=1; nr<month-1; ++nr)
daynumber=daynumber+numberdaysmonth(year, month);
daynumber=daynumber+day;
return daynumber;
}

int numberdaysmonth(int year, int month)
{
int loop=0;
int days=0;
if (year%4)
{
if (year%100)
{
if (year%400)
loop=0;
else
loop=1;
}
else
loop=0;
}
else
loop=1;

if(loop==0)
switch (month)
{
case 1: days=31; return 31;
case 2: days=29; return 28;
case 3: days=31; return 31;
case 4: days=30; return 30;
case 5: days=31; return 31;
case 6: days=30; return 30;
case 7: days=31; return 31;
case 8: days=31; return 31;
case 9: days=30; return 30;
case 10: days=31; return 31;
case 11: days=30; return 30;
case 12: days=31; return 31;
default: cout << "Incorrect Data"; return 0;
}
else
switch (month)
{
case 1: days=31; return 31;
case 2: days=28; return 29;
case 3: days=31; return 31;
case 4: days=30; return 30;
case 5: days=31; return 31;
case 6: days=30; return 30;
case 7: days=31; return 31;
case 8: days=31; return 31;
case 9: days=30; return 30;
case 10: days=31; return 31;
case 11: days=30; return 30;
case 12: days=31; return 31;
default: cout << "Incorrect Data"; return 0;
}
}

int dayweek(int year, int month, int day)
{
int daynumber=0;
daynumber=numberdays(year, month, day)%7;
writeweeksday(daynumber);
return daynumber;
}

void calender(int &year, int &month)
{
int day=0;
for(float nr=0;++nr<=1;)
{
cout << setw(8) << "Sun" << setw(8) << "Mon" << setw(8) << "Tues" << setw(8) << "Wed" << setw(8) << "Thurs" << setw(8) << "Fri" << setw(8) << "Sat";
cout << endl;
}
for(float nr=0;++nr<=1;)
{

for (float i=-1;++i<=6;){
if (i==numberdays(year, month, 1)%7){
day=1;
}
else if (i>numberdays(year, month, 1)%7)
day=day+1;
if (day==0){
cout << setw(8) << "";}
else
cout << setw(8) << day;
}
cout << endl;
}
for(float nr=0;++nr<=5;)
{

for (float i=-1;++i<=6;){
day=day+1;
cout << setw(8) << day;
if (day==numberdaysmonth(year, month))
break;
}
cout << endl;
if (day==numberdaysmonth(year, month))
break;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!