Structures and Records

I am writing a program to be able to compare birthdays of 2 people to determine who is older. I need to use structures and records which i am very unfamiliar with. I have so code done but I'm not sure where I am going with it.

#include <iostream>

using namespace std;

struct printDate {
int day;
int month;
int year;
};
printDate getDate (int date)
{
printDate toreturn;
toreturn.day= date % 30;
toreturn.month= date % 12;
toreturn.year= date % 2000;
}
int compareDate ( printDate birthday)
{
int date;
int date2;
date = date.day;
if (date.day > date2.day)






int main()
{
int date1;
int date2;
cout << "Please enter your birthday in the format dd/mm/yyyy" << endl;
cin >> date1;
cout << "Please enter another birthday in the formate dd/mm/yyyy" << endl;


return 0;
}


any help would be much appreciated
closed account (iw0XoG1T)
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;

struct printDate {
    printDate(int d, int m, int y):
        day(d),month(m),year(y){
        value_of_date=year*10000 + month*100 + day;
        }
    int day;
    int month;
    int year;
    int value_of_date;
};

int main(){

    printDate adate(25,11,2009);
    printDate adate2(1,1,1900);
    if(adate.value_of_date<adate2.value_of_date)cout<<"the oldest date is: "
        <<adate.month<<'/'<<adate.day<<'/'<<adate.year<<endl;
    else cout<<"the oldest date is: "
        <<adate2.month<<'/'<<adate2.day<<'/'<<adate2.year<<endl;
    
    return 0;
}


I edited this post the original had a error on line 7
Last edited on
A record and a struct are the same thing. A structure is just a collection of other data, just like you have.
1
2
3
4
5
struct printDate {
  int day;
  int month;
  int year;
};
Here you have three data stored under one name. (With a type of "printDate").

Your function and variable names need a little help. For example, "getDate" would be better named something like "parseDate" or "intToDate" or the like. Likewise, your "printDate" structure might be better named just "Date".

Don't forget to return things from functions that return a value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Date {
  int day;
  int month;
  int year;
};

Date intToDate( int date )
{
  Date result;
  result.day   = ...;
  result.month = ...;
  result.year  = ...;
  return result;
}

For the "getDate"/"intToDate" function, your math needs a little help. Remember that "%" is the remainder operator, returning the remainder of an integer division. For example,
1
2
3
4
5
6
7
8
9
  9 / 1 = 9;  9 % 1 = 0
  9 / 2 = 4;  9 % 2 = 1
  9 / 3 = 3;  9 % 3 = 0
  9 / 4 = 2;  9 % 4 = 1
  9 / 5 = 1;  9 % 5 = 4
  9 / 6 = 1;  9 % 6 = 3
  9 / 7 = 1;  9 % 7 = 2
  9 / 8 = 1;  9 % 8 = 1
  9 / 9 = 1;  9 % 9 = 0
The way you currently have it coded, you are extracting the same number as different things.

That said, you probably want to write a function to read the birthdate as three separate integers that you put directly into the "Date" struct.
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
Date askBirthday()
{
  Date result;

  cout << "year: " << flush;
  cin >> result.year;

  cout << "month (1-12): " << flush;
  cin >> result.month;

  cout << "day (1-31): " << flush;
  cin >> result.day;

  return result;
}

...

int main()
{
  Date yourBirthday;
  Date anotherBirthday;

  cout << "Please enter your birthday\n";
  yourBirthday = askBirthday();

  ...

Finally, when comparing dates, compare the most significant numbers first. The year is more important than the month, and the month is more important than the day.

Hope this helps.

[edit] Wow, after all that someone did the OP's homework for him before I could hit the Submit button. Way to go!
Last edited on
Thank you! That helped so much. I am going to go give it a try and see what I can come up with.
Topic archived. No new replies allowed.