Boolean Function to validate birthday

Viewed 40

I'm new to programming and got a task to do, but I'm kinda confused how to use the boolean function and how to understand the parameter.

I get into it for the most part when I see a starting point, but for "CustomerAisOlder" and "CustomerHasBirthdayToday" I'm kinda lost how to get it started.

#include <iostream>
#include <string>


using namespace std;

//  Create a new project with a class named "Customer" 
class Customer {

public:
    string firstname;
    string surname;
    string email;

    int dayOfBirth;
    int monthOfBirth;
    int yearOfBirth;
    int age;

    void birthday() {
        cout << "Input your birthday (DD/MM/YYYY): " << endl;
        cin >> dayOfBirth;
        cin >> monthOfBirth;
        cin >> yearOfBirth;
    }
    void inputAge() {
        cout << "Enter your age: " << endl;
        cin >> age;
    }
    void displayBirthday() {
        cout << "Birthday: " << dayOfBirth << "." << monthOfBirth << "." << yearOfBirth << endl;
    }

};

//  Check if Customer A is older than Customer B
bool CustomerAisOlder(Customer A, Customer B)
{

}


//  Check if Customer has birthday today
bool CustomerHasBirthdayToday(int currentDay, int currentMonth , int currentYear)
{

}

int main() {

    // Object
    Customer customer1;

    customer1.firstname = "John";
    customer1.surname = "Thompson";
    customer1.email = "Johnny318@gmail.com";

    customer1.birthday();
    cout << "\nName: " << customer1.firstname << " " << customer1.surname << endl;
    cout << "E-Mail: " << customer1.email << endl;
    customer1.displayBirthday();

}
1 Answers

For boolean functions, you must return either true or false. So, in your function, you must either return true or false.

For CustomerAisOlder, you must compare the two customers you pass into the function. One way you could check which person is older is by first comparing the year they were born. If they were born in the same year, we check the month, and if they were born in the year and month, we compare the day. Remember that the smaller the values, the older they are. This is one way to implement this:

bool CustomerAisOlder(Customer A, Customer B)
{
    if (A.yearOfBirth < B.yearOfBirth)
    {
        return true;
    }
    else if (A.yearOfBirth > B.yearOfBirth)
    {
        return false;
    }
    else
    {
        if (A.monthOfBirth < B.monthOfBirth)
        {
            return true;
        }
        else if (A.monthOfBirth > B.monthOfBirth)
        {
            return false;
        }
        else
        {
            if (A.dayOfBirth < B.dayOfBirth)
            {
                return true;
            }
            else if (A.dayOfBirth > B.dayOfBirth)
            {
                return false;
            }
            else
            {
                return false;
            }
        }
    }
}

Or we can shrink it into:

bool CustomerAisOlder(Customer A, Customer B)
{
    if (A.yearOfBirth < B.yearOfBirth) return true;
    else if (A.yearOfBirth > B.yearOfBirth) return false;
    else
    {
        if (A.monthOfBirth < B.monthOfBirth) return true;
        else if (A.monthOfBirth > B.monthOfBirth) return false;
        else
        {
            if (A.dayOfBirth < B.dayOfBirth) return true;
            else if (A.dayOfBirth > B.dayOfBirth) return false;
            else return false;
        }
    }

For CustomerHasBirthdayToday, you must also pass in the customer in question, or else we don't know which Customer instance to check the birthday for. To implement the code, you must check if the month and day is the same as the customer's birthday. We also don't actually need the year of birth, since that doesn't change the customer's birthday.

This is one way you can implement the code:

bool CustomerHasBirthdayToday(customer A, int currentDay, int currentMonth)
{
    if (A.monthOfBirth == currentMonth && A.dayOfBirth == currentDay)
    {
        return true;
    }
    
    return false;
}
Related