Long boring problem

If anyone is bored enough to look through the code, here it is. No idea what is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Date.h
class Date {
private:
	int day;
	int month;
	int year;

public:
	Date(int = 1, int = 1, int = 1500); // Set the day, month and year
	void set(int, int, int); // Set the day, month and year
	void setDay(const int _d); // Set the day
	void setMonth(const int _m); // Set the month
	void setYear(const int _y); // Set the year
	void print() const; // Print the date in the format day/month/year
	int getDay() const; // Return the day
	int getMonth() const; // Return the month
	int getYear() const; // Return the year
	int isLeap() const; // Return 0 if the year is not a leap year otherwise return 1
	int daysInMonth() const; // Return how many days are in the current month
	int daysPassedInYear() const; // Return how many days are passed in the current year
	int daysRemainingInYear() const; // Return how many days are remaining in the current year
	Date addDays(const int) const;  // Add the given number of days to the date and return the new date as a Date object.

};


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
115
116
117
118
119
120
121
122
123
124
125
126
127
//Date.cpp
#include <iostream>
using namespace std;
#include "Date.h"

Date :: Date(int _day, int _month, int _year)
{
	day=_day;
	month=_month;
	year=_year;

}

void Date :: set(int _day, int _month, int _year)
{
	day=_day;
	month=_month;
	year=_year;

}

void Date :: setDay(const int _day)
{
	day=_day;
}

void Date :: setMonth(const int _month)
{
	month=_month;
}

void Date :: setYear(const int _year)
{
	year=_year;
}

void Date :: print() const
{
	cout<<day<<"/"<<month<<"/"<<year<<endl;
}

int Date :: getDay() const
{
	return day;
}

int Date :: getMonth() const
{
	return month;
}

int Date :: getYear() const
{
	return year;
}

int Date :: isLeap() const
{
	if(year%4==0)
		if(year%100==0)
			if(year%400==0)
				return 1;
			else return 0;
		else return 0;
	else return 0;
}

int Date :: daysInMonth() const
{
	if (month==4 || month==6 || month==9 || month==11)
		return 30;
	else if (month==2 && isLeap()==1)
		return 28;
	else
		return 29;
}

int Date :: daysPassedInYear() const
{
	int month_days[12];
	int temp=0;
	int daysPassed;
	for (int i=0; i<12; i++)
	{
		month_days[i]=daysInMonth();
	}

	for (int j=0; j<month; j++)
	{
		temp+=month_days[j];
	}

	daysPassed=temp+day;
	return daysPassed;
}

int Date :: daysRemainingInYear() const
{
	int daysRemaining;
	if(isLeap()==1)
		daysRemaining=366-daysPassedInYear();
	else
		daysRemaining=365-daysPassedInYear();

	return daysRemaining;
}

Date Date :: addDays(const int _days) const
{
    int tempday=getDay();
    int tempmonth=getMonth();
    int tempyear=getYear();

	tempday+=_days;
	if(tempday > daysInMonth()){
		tempday=1;
		tempmonth++;
	}
	if(tempmonth > 12){
		tempmonth=1;
		tempyear++;
    }

    Date(tempday,tempmonth,tempyear);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//exDate.h
#include "Date.h"
#include "string"

class exDate : public Date{
private:
	string strMonth;
public:
	exDate(int = 1, int = 1, int = 1500); // Initialize the base class, and also the member data strMonth with the right string using the month parameter.
	void setMonth(int); // Call the base class function to set the month and also update strMonth
	string getMonth() const; // Return the month in the string format
	void print() const; // Print the date in the form of e.g. March 18, 2006

};


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
//exDate.cpp
#include <iostream>
using namespace std;
#include "exDate.h"

exDate :: exDate(int _day, int _month, int _year) : Date(_day,_month,_year){
	switch (_month){
		case 1:
			strMonth="January";
			break;
		case 2:
			strMonth="February";
			break;
		case 3:
			strMonth="March";
			break;
		case 4:
			strMonth="April";
			break;
		case 5:
			strMonth="May";
			break;
		case 6:
			strMonth="June";
			break;
		case 7:
			strMonth="July";
			break;
		case 8:
			strMonth="August";
			break;
		case 9:
			strMonth="Septempber";
			break;
		case 10:
			strMonth="October";
			break;
		case 11:
			strMonth="November";
			break;
		case 12:
			strMonth="December";
			break;
		default:
			cout<<"Error 20029. Windows is shutting down.";
	}
}

void exDate :: setMonth(int _month){

	Date :: setMonth(_month);
		switch (_month){
		case 1:
			strMonth="January";
			break;
		case 2:
			strMonth="February";
			break;
		case 3:
			strMonth="March";
			break;
		case 4:
			strMonth="April";
			break;
		case 5:
			strMonth="May";
			break;
		case 6:
			strMonth="June";
			break;
		case 7:
			strMonth="July";
			break;
		case 8:
			strMonth="August";
			break;
		case 9:
			strMonth="Septempber";
			break;
		case 10:
			strMonth="October";
			break;
		case 11:
			strMonth="November";
			break;
		case 12:
			strMonth="December";
			break;
		default:
			cout<<"Error 20029. Windows is shutting down.";
	}
}

string exDate :: getMonth() const{
	return strMonth;
}

void exDate :: print() const{

	cout<<strMonth<<" ";
	Date :: getDay();
	cout<<", ";
	Date :: getYear();
	cout<<endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//weekDay.h
#include<string>
class weekDay{
private:
	string day;
public:
	weekDay(string = "Mon"); // Initialize the day
	void print() const; // Print the day
	void set(string); // Set the day
	string get() const; // Return the day
	string getNext() const; // Return the next day
	string getPrevious() const; // Return the previous day
	string add(const int) const; // Add a number of days to the day and return
}


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
//weekDay.cpp
#include<iostream>
using namespace std;
#include "weekDay.h"


weekDay::weekDay(string weekday)
{
	day=weekday;
}

void weekDay :: set(string weekday){
	day=weekday;
}

void weekDay :: print() const{
	cout<<day<<" ";
}

string weekDay :: get() const{
	return day;
}

string weekDay :: getNext() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day)
			if(i==6)
				day=weekday[0];
			day=weekday[i+1];
	}
	return day;
}

string weekDay :: getPrevious() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day)
			if(i==0)
				day=weekday[6];
			day=weekday[i-1];
	}
	return day;
}

string weekDay :: add(const int _days) const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};

	for (int i=0; i<7; i++){
		int j=i;

		if (weekday[i]==day){
			for(int k=0; k<(_days-i); k++){
				if (j==7)
					j=0;
				j++;
			}}
			day=weekday[j];
			return day;
			break;
	}
}



hi,
it could help alot if you at lest tell us what kind of errors are you reciving by pasting output errors.
thanks.
1>------ Build started: Project: calendar_project, Configuration: Debug Win32 ------
1>  weekDay.cpp
1>c:\users\amr\documents\visual studio 2010\projects\assignment_3\assignment_3\weekday.cpp(31): warning C4715: 'weekDay::getNext' : not all control paths return a value
1>c:\users\amr\documents\visual studio 2010\projects\assignment_3\assignment_3\weekday.cpp(41): warning C4715: 'weekDay::getPrevious' : not all control paths return a value
1>c:\users\amr\documents\visual studio 2010\projects\assignment_3\assignment_3\weekday.cpp(59): warning C4715: 'weekDay::add' : not all control paths return a value
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>c:\users\amr\documents\visual studio 2010\Projects\calendar_project\Debug\calendar_project.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The code compiles fine (well, a few warnings that will cause you enormous trouble if you try to use the return values from the functions weekDay::getNext, weekDay::getPrevious or weekDay::add).

On to the actual error:

You don't have a main function. If this is to be an executable, you must have a main function. It's where the program starts running.

If this is to be a library, you need to tell your linker that it is to be a library, so it doesn't complain about not finding the main.

Learn this error well: unresolved external symbol. You will see it again. It means that the linker, after the code has been compiled, can't find something it needs.
Last edited on
Yes sorry I forgot to post the main because it given as part of the assignment. Here it is, the code compiles correctly but the output is completely wrong, don't understand why... Here is the main, the output, and the actual output:

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
#include <iostream>
using namespace std;
#include "exDate.h"
#include "weekDay.h"

int main()
{
	Date d1(18, 3, 2006);
	cout<<"The date is: ";
	d1.print();
	if ( !d1.isLeap() )
		cout<<"Not a leap year\n";
	cout<<d1.daysPassedInYear()<<" days are passed in the year."<<endl;
	cout<<d1.daysRemainingInYear()<<" days are remaining in the year."<<endl;
	cout<<"After adding 25 days, the date is: ";
	d1.addDays(25).print();
	exDate ed1(18, 3, 2000);
	cout<<endl<<"Extended date: ";
	ed1.print();
	cout<<ed1.daysPassedInYear()<<" days are passed in the year."<<endl;

	weekDay w;
	cout<<"\nToday is: ";
	w.print();
	cout<<endl<<"The previous day is: "<<w.getPrevious()<<endl;
	cout<<"The next day is: "<<w.getNext()<<endl;
	cout<<"After 7 days it is: "<<w.add(7)<<endl;
	cout<<"After 4 days it is: "<<w.add(4)<<endl;
	cout<<"Three days ago it was: "<<w.add(-3)<<endl;
	cout<<"After 28 days it is: "<<w.add(28)<<endl;
	cout<<endl;

	return 0;
} 


Output displayed:
The date is: 18/3/2006
Not a leap year
105 days are passed in the year.
260 days are remaining in the year.
After adding 25 days, the date is: 1/4/2006

Extended date: March ,
105 days are passed in the year.

Today is: Mon
The previous day is:
The next day is: Sun


Output it should display:
The date is: 18/3/2006
Not a leap year
77 days are passed in the year.
288 days remaining in the year.
After adding 25 days, the date is: 12/4/2006

Extended date: March, 18, 2000
78 days are passed in the year

Today is: Mon
The previous day is: Sun
The next day is: Tue
After 7 days it is: Mon
After 4 days it is: Fri
Three days ago it was: Fri
After 28 days it is: Mon
Adjusting you indenting a little bit, to make the scoping a bit clearer (to me)...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string weekDay :: add(const int _days) const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};

	for (int i=0; i<7; i++){
		int j=i;

		if (weekday[i]==day){
			for(int k=0; k<(_days-i); k++){
				if (j==7)
					j=0;
				j++;
			}
		}
		day=weekday[j]; // (a) you shouldn't be able to assign to a member variable
		// in a const method, and (b) shouldn't be able to assign a string to an int?
		return day; // should this be in the if statement?
		break;
	}

	// should there be another return here?
}


Have you made changes to this code since you first posted it??

Andy
Last edited on
PS I would use brackets in this case, too


1
2
3
4
5
6
7
8
9
10
string weekDay :: getPrevious() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day)
			if(i==0)
				day=weekday[6];
			day=weekday[i-1];
	}
	return day;
}


Probably


1
2
3
4
5
6
7
8
9
10
11
string weekDay :: getPrevious() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day) {
			if(i==0) // I do omit brackets when there is just a single line following
				day=weekday[6];
			day=weekday[i-1];
		}
	}
	return day;
}

Have you made changes to this code since you first posted it??


yes:

This part was giving wrong values so I fixed it:
1
2
3
4
5
6
7
8
9
10
11
int Date :: daysInMonth() const
{
	if (month==4 || month==6 || month==9 || month==11)
		return 30;
	else if (month==2 && isLeap()==1)
		return 28;
	else if (month==2 && isLeap()==0)
		return 29;
	else
		return 31;
}


This is the part you were talking about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string weekDay :: add(const int _days) const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};

	for (int i=0; i<7; i++){
		int j=i;

		if (weekday[i]==day){
			for(int k=0; k<(_days-i); k++){
				if (j==7)
					j=0;
				j++;
			}

		}
		return weekday[j];
		break;
	}
}


I think that in this part something is wrong because i dont think that weekday[i] is comparing the string with day...
daysInMonth() never returns 31, so it will getting the length of the month wrong for Jan, March, May, July, Aug, Oct, and Dec.

1
2
3
4
5
6
7
8
9
int Date :: daysInMonth() const
{
	if (month==4 || month==6 || month==9 || month==11)
		return 30;
	else if (month==2)
		return (isLeap()==1) ? 29 : 28;

	return 31;
}
And I don't quite get how your daysInMonth() works here? Should it take the month index, rather than use the month member variable?

You loop is adding 12 x the length of the current month to get daysPassed, rather than the length of each month.

And temp looks redundant? Just use daysPassed instead?

And why use array, rather than just cumulate the days in a single loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Date :: daysPassedInYear() const
{
	int month_days[12];
	int temp=0;
	int daysPassed;
	for (int i=0; i<12; i++)
	{
		month_days[i]=daysInMonth(); // returns length of current month?
	}

	for (int j=0; j<month; j++)
	{
		temp+=month_days[j];
	}

	daysPassed=temp+day;
	return daysPassed;
}
Last edited on
if (weekday[i]==day) is wrong because the day member variable is an int, and weekday is an array of strings.

I am surprised this compiles!?

Should this just be if (i==day)
Last edited on
Topic archived. No new replies allowed.