calendar

few years ago I wrote the small codes on visual C++ which was printing calendar of any years and any month. Now I am trying to compile again the same codes on Ubuntu and there is the error: "segmentation fault". I never found.mistake.

could you please advice me something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
using namespace std;

void clear(void);

int main()
{
	char ch;
	int year,mon,shy;
	int ivoid, maxday;

	int day[31]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
		16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

	char month[12][10]=
		{"January","February","March","April","May","June",
		"July","August","September","October","November","December"};

	char weekday[7][10]={"Monday","Tuesday","Wednesday","Thursday",
		"Friday","Saturay","Sunday"};

	year=1984;
	mon=7;
	const cyear=2008;
	const cnon=1;
	ivoid=1;
	do{
	cout<<"Enter year: ";
	cin>>year;
	clear();
	cout<<"Enter number of month: ";
	cin>>mon;
	clear();

	if((year-cyear)==0)
		shy=0;
	else if((year-cyear)>0)
	{
		shy=(year-cyear)+int((year-cyear)/4);
		if ((mon==1||mon==2)&&year%4!=0) shy++;
	}
	else if((year-cyear)<0)
	{
		shy=(year-cyear)-int((cyear-year)/4);
		if ((mon!=1 && mon!=2)&&year%4!=0) shy--;
	}

	if     (mon==1)  ivoid=shy+1;
	else if(mon==2)  ivoid=shy+4;
	else if(mon==3)  ivoid=shy+5;
	else if(mon==4)  ivoid=shy+1;
	else if(mon==5)  ivoid=shy+3;
	else if(mon==6)  ivoid=shy+6;
	else if(mon==7)  ivoid=shy+1;
	else if(mon==8)  ivoid=shy+4;
	else if(mon==9)  ivoid=shy+0;
	else if(mon==10) ivoid=shy+2;
	else if(mon==11) ivoid=shy+5;
	else if(mon==12) ivoid=shy+0;

	while(ivoid<0)
		ivoid=ivoid+7;
	ivoid=ivoid%7;

	if (mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
		maxday=31;
	else if(mon==4||mon==6||mon==9||mon==11)
		maxday=30;
	else if(mon==2)
	if(year%4==0)
		maxday=29;
	else
		maxday=28;


	cout<<" .................. "<<month[mon-1]<<", "<<year<<" ...................\n";
	cout<<"....................................................\n";
	cout<<"Monday Tuesday Wednes Thursday Friday Saturay Sunday\n";


	for (int v=0;v<ivoid; v++)
		cout<<"\t";

	for(int i=0; i<maxday;i++)
	{
		cout<<day[i]<<"\t";
		if((ivoid+i+1)%7==0)
			cout<<"\n\n";
	}

	cout<<"\n";

	cout<<"Enter any kay for continue.   < Q > - quite\n";
	cin>>ch;

	}
	while(ch!='q'&& ch!='Q');

	return 0;
}



void clear(void)
{
	if(!cin)
	{
		cin.clear();
		while (cin.get()!='\n')
		continue;
		cout<<"Bad input!\a\n";
	}
}
This code does not compile on any platform, because of lines 24 and 25. Do you see what's wrong with them?

Aside from that, your code should work just fine. It works on the related darwin platform.

-Albatross
Last edited on
Correct, on line 24 and 25 is constants without declaring type of constants. I just change const cyear=2008; and const cnon=1; in const int cyear=2008; and const int cnon=1; and now it is working fine!

thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
using namespace std;

void clear(void);

int main()
{
	char ch;
	int year,mon,shy;
	int ivoid, maxday;

	int day[31]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
		16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};

	char month[12][10]=
		{"January","February","March","April","May","June",
		"July","August","September","October","November","December"};

	char weekday[7][10]={"Monday","Tuesday","Wednesday","Thursday",
		"Friday","Saturay","Sunday"};

	year=1984;
	mon=7;
	const int cyear=2008;
	const int cnon=1;
	ivoid=1;
	do{
	cout<<"Enter year: ";
	cin>>year;
	clear();
	cout<<"Enter number of month: ";
	cin>>mon;
	clear();

	if((year-cyear)==0)
		shy=0;
	else if((year-cyear)>0)
	{
		shy=(year-cyear)+int((year-cyear)/4);
		if ((mon==1||mon==2)&&year%4!=0) shy++;
	}
	else if((year-cyear)<0)
	{
		shy=(year-cyear)-int((cyear-year)/4);
		if ((mon!=1 && mon!=2)&&year%4!=0) shy--;
	}

	if     (mon==1)  ivoid=shy+1;
	else if(mon==2)  ivoid=shy+4;
	else if(mon==3)  ivoid=shy+5;
	else if(mon==4)  ivoid=shy+1;
	else if(mon==5)  ivoid=shy+3;
	else if(mon==6)  ivoid=shy+6;
	else if(mon==7)  ivoid=shy+1;
	else if(mon==8)  ivoid=shy+4;
	else if(mon==9)  ivoid=shy+0;
	else if(mon==10) ivoid=shy+2;
	else if(mon==11) ivoid=shy+5;
	else if(mon==12) ivoid=shy+0;

	while(ivoid<0)
		ivoid=ivoid+7;
	ivoid=ivoid%7;

	if (mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)
		maxday=31;
	else if(mon==4||mon==6||mon==9||mon==11)
		maxday=30;
	else if(mon==2)
	if(year%4==0)
		maxday=29;
	else
		maxday=28;


	cout<<" .................. "<<month[mon-1]<<", "<<year<<" ...................\n";
	cout<<"....................................................\n";
	cout<<"Monday Tuesday Wednes Thursday Friday Saturay Sunday\n";


	for (int v=0;v<ivoid; v++)
		cout<<"\t";

	for(int i=0; i<maxday;i++)
	{
		cout<<day[i]<<"\t";
		if((ivoid+i+1)%7==0)
			cout<<"\n\n";
	}

	cout<<"\n";

	cout<<"Enter any kay for continue.   < Q > - quite\n";
	cin>>ch;

	}
	while(ch!='q'&& ch!='Q');

	return 0;
}



void clear(void)
{
	if(!cin)
	{
		cin.clear();
		while (cin.get()!='\n')
		continue;
		cout<<"Bad input!\a\n";
	}
}

Topic archived. No new replies allowed.