Fill in missing functions?

Hi. As part of a homework assignment, I am supposed to fill in missing functions without changing main to allow the following code to compile: (This isn't the entire assignment- I also have to write code that gives the number of days between two dates but I know how to do that part)

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
 #include <iostream>

using namespace std;

int main()
{
	int day, month, year;
	char c;
	
	cout << "Enter a start date: " << endl;
	cin >> month >> c >> day >> c >> year;
	
	date start = date(month, day, year); //somehow I need to initialize start outside of main
	
	cout << "Enter an end date: " << endl;
	cin >> month >> c >> day >> c >> year;
	
	date end = date(month, day, year); //somehow I need to initialize end outside of main
	
	int duration = end - start;
	
	cout << "The number of days between those two dates are: " << duration << endl;
	
	return 0;
}


I put a void function on top that looks like this:
void date (int start, int end)
{

}
But that didn't help with getting the code to compile. Can anyone help explain what I'm supposed to do? I guess my main question is how(and where) do I fill in functions that will allow me to use "start" and "end", and is void the right type?
I just don't understand how I am supposed to do it without changing main()
Last edited on
date start = date( month, day, year) means that you have to define a 'date' class/struct.

int duration = end - start means that you must implement a subtraction of two 'date' class/structs. Therefore you must it overload by a 'operator-' function.
Examine closely how date is used on line 13:

date start = ... ;

This line creates a variable named "start" of type "date". date is not a built-in type (e.g., int, bool, char), which means it's a user defined type. You'll need to define the "date" type, which can be done using a struct or class.

... = date(month, day, year);

This is a call to the constructor function of your date class/struct. It indicates that a "date" object expects it'll be passed three ints as arguments.

I see where your thought process was going with the void date() function, but in this case the exercise will need a class or struct rather than a single function.
Thanks! That definitely helped. I was getting nowhere using regular functions. Now I'm having trouble with overloading the - operator, here's what I have for the class:

1
2
3
4
5
6
7
8
class date{
public:
date(int month, int day, int year);
friend date operator-(int date& start, int date& end);
private:
int month;
int day;
int year;


Now I'm trying to write the function for overloading the operator. This is a new concept for my class and I'm new to C++ so I'm having trouble. This is what I called the function:

1
2
3
4
 date operator-(int date& start, int date& end)
{

}


What do I need inside it? I've been looking in my textbook and online but it's so hard for me to understand object overloading..

You have declared operator- wrong. When you subtract two dates, you don't get another date, you get a duration. So it should be something like
1
2
3
4
5
class date {
...
    duration operator-(const date &d);
...
};


What should a duration be? Perhaps an integer representing the number of days.

Also, consider changing the internal representation of class date. Rather than storing the month/day/year, I'd store something like the Julian Day Number (JDN). If you store that, and write functions to convert back and forth between month/day/year and JDN then the code for operator-() becomes trivial.
Topic archived. No new replies allowed.