Create a Person class to represent a person's contact information. Your Person class must consist of the following:

PRIVATE members:
string firstName,lastName;
birthDate //you get to choose the data type, but the type you choose must be able to maintain a
// 1 day resolution

PUBLIC members:

1. Default constructor that sets the first name to "", last name to "", and the date of birth to January 1st, 2000.

2. Copy constructor

3. Initialization constructor that takes 3 arguments - 1 const string for each first name and last name, and a third argument of the type you use for your Person's age.

4. Three Accessor methods that each return one of the private data members - the first name, last name, and date of birth as strings (i.e. string type). In other words, you must implement these methods:

string getFirstName() const;
string getLastName() const;
string getBirthDate() const;

5. A method that compares the age of this person with another person's age, returning the number of days old this person is to the others. This method must have the following prototype:

int ageDifference(const Person& p); // returns #days older this person is than Person p

FRIEND FUNCTIONS:

ostream& operator<<(ostream& o, const Person& p); bool operator==(const Person& p1, const Person& p2); //returns true if p1 and p2 have same //first and last names AND the same birthdate. bool operator<(const Person& p1, const Person& p2); // returns true if p1 comes first alphabetically // look at last names first, then first names if // the last names are the same bool operator>(const Person& p1, const Person& p2); //returns true if p2 comes first alphabetically


I have no problems with steps 1 through 4. When it came down to step 5, I have no clue what to do. My problem is constructing ageDiffference and other methods that can support it. I am stuck with a string. (ex "01/01/2001") How can I do subtraction with strings?

Person() : fN(""), lN(""),BD(()){}
Person(const string& fn,const string& ln,const string& dob):
	    fN(fn), lN(ln), bD(dob) {}

int ageDifference(const Person& p);

For example I want my output for this case to be Micheal is 365 days older.
Assume every month is 30 days. No leap years.

int main{
Person a("Michael", "", "7/25/1980");
Person b("Marius", "", "6/25/1981");
cout << a.getFirstName() << " is " << a.ageDifference(b) << " days older than " << b.getFirstName() << endl;
}

I've been thinking about using tt_time but I have no clue how that works.

sixthsensess commented: ` +0

Dani AI

Generated

Agree with that the simplest way is to turn the date string into integers and do arithmetic. Building on 's parsing idea, use a small helper that parses "M/D/YYYY" (or "MM/DD/YYYY") into month/day/year ints, converts that to a canonical "days since epoch" using months = 30 days and years = 365 days, and then subtracts those totals in ageDifference. This keeps ageDifference trivial and easy to test.

#include <stdexcept>
#include <cstdio>
#include <string>

static int daysFromMDY(const std::string &s) {
    int m,d,y;
    if (std::sscanf(s.c_str(), "%d/%d/%d", &m,&d,&y) != 3)
        throw std::invalid_argument("bad date");
    return y*365 + (m-1)*30 + (d-1);
}

int Person::ageDifference(const Person &p) const {
    // positive => *this is older than p
    return daysFromMDY(p.getBirthDate()) - daysFromMDY(this->getBirthDate());
}

Notes: validate parsed ranges (month 1-12, day 1-31) or use std::stoi/istringstream if preferred. The sign convention above returns a positive number when the caller is older. With the sample dates (Michael 7/25/1980, Marius 6/25/1981) the math (using 30-day months, no leap years) gives 335 days older — the 365-day result would occur if the second birthdate were 7/25/1981. Use long long if expecting extremely large year values.

Recommended Answers

All 3 Replies

Once again, the solution is to implement a string2int function. It will convert digits from string form to int form (if they are given in the right form). String2int implementations are found on this forum.

You might want to implement your own function which would also separate the months/days/years using a '/' delimiter and multiply by 30/1/365 respectively

#include <sstream>
#include <iostream>
#include <string>

unsigned long diff(std::string);

int main ()  {
   std::cout << diff("7/12/1993");
 }

unsigned long diff(std::string date)  {   
   int dmy[3];
   std::string hld[3];
   std::string::iterator prs=date.begin();   

   for (int n=0;n<3;n++)  {
      while (prs!=date.end())  {
         if ((*prs)=='/') {
             ++prs;  
             break;
           }
         hld[n]+=(*prs);
         std::istringstream i(hld[n]);
         i >> dmy[n];
         ++prs;
        }
     }
    return dmy[0]+(dmy[1]*31)+(dmy[2]*365);
  }

Thanks alot caut_baia. The example you gave was very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.