making a function using structures?

problem:

4
a. Write a c++ function named larger() that returns the later date of any two dates passed to it. For example, if the dates 10/9/2015 and 11/3/2015 are passed to larger(), the second date is returned.

b. Include the larger() function written for exercise 4a in a complete program. Store the Date structure returned by larger() in a separate Date structure and display the member values of the returned Date.

I'm trying to do a but this is what I have so far. I probably have the wrong idea to even trying to solve this problem. But here it is.
------------------------------------------------------------------------
#include <iostream>
using namespace std;

struct date
{
int *month;
int *day;
int *year;
}dateone,datetwo;

void larger(int*,int*,int*,int*,int*,int*);
int main ()
{
cout << "Enter the first date in the order of month, day and year." << endl;
cin >> *dateone.month >> *dateone.day >> *dateone.year;
cout << "Enter the second date in the order of month, day, and year." << endl;
cin >> *datetwo.month >> *datetwo.day >> *datetwo.year;
date emp;
larger(dateone.month,dateone.day,dateone.year,datetwo.month,datetwo.day,datetwo.year);

system("pause");
return 0;
}

void larger(int *d1month, int *d1day, int *d1year, int *d2month, int *d2day, int *d2year)
{
if(*d1month>*d2month && *d1year>*d2year)
cout << *d1month << "/" << *d1day << "/" << *d1year;
if(*d1month>*d2month && *d1year==*d2year)
cout << *d1month << "/" << *d1day << "/" << *d1year;
if(*d1month==*d2month && *d1day>*d2day)
cout << *d1month << "/" << *d1day << "/" << *d1year;
else
cout << *d2month << "/" << *d1day << "/" << *d2year;

return;
}

----------------------------------------------------------------------
I'm really new to programming, can anyone help me out ASAP please!
1. Use code wrap [ code] Code here [ /code]
2. Indent
3. Point out where the problem occurs

Since you're new, I'll try to help even though this isn't properly done. just keep the three above tips in mind for later posts.

Your first problem is that you declare larger as a void. If you are going to be returning a value, you can't declare it as void. You have to declare it as the value type you will be returning, which looks like your struct date in this assignment. Second, why are you declaring everything as pointers? This assignment can be done just fine without any pointers.

You have the correct general idea, but instead of displaying the larger date in larger() by using cout, return it by using return(). In the main function, if you put cout << larger(info), it will display whatever you return in larger. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int add(int, int);

int main()
{
    cout << add(5, 5);
}

int add(int x, int y)
{
    return x + y;
}


Would display 10, because x + y (5 + 5 in this example) is 10.
Topic archived. No new replies allowed.