Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to calculate the date of a day that is n days after a given

ID: 3669775 • Letter: W

Question

Write a C++ program to calculate the date of a day that is n days after a given day: Oct 1st 2015 + 5 days = Oct 6th 2015

Use the functions: DayPlusN( month, day, year, and n)

                            DayNo2Date(dayNo, day, month, year)

Do not put your NextDay function in a loop.

I have the following fucntions already to check for leap year:

bool IsLeapYear(int y)

days in a month

int DaysInMonth(int m, int y)

and to check for the next day date after whatever date given. for instance, I input 2/3/2016 will output 2/4/2016. ( one day ahead only)

void NextDate(int &m, int&d,int &y)

Explanation / Answer

#include <iostream>
using namespace std;

// functions: get/show date
void GetDate(int &m, int &d, int &y);
void ShowDate(int m, int d, int y);
bool NextDate(int &m, int &d, int &y);

// function: user input validation
bool DateIsValid(int &m, int &d, int &y);
// function: leap year validation
bool IsLeapYear(int y);
// function: calculate number of days in Month
int DaysInMonth (int m, int y);
/*
*
*/
int main() {
    // initialize variable: month, day, year
    int m, d, y;
    char ans;
  
    do
    {
        GetDate(m, d, y);
        if (!DateIsValid(m, d, y)) {
            cout << "INVALID INPUT!" << endl;
            ans = 'y';
            continue;
        }
      
        cout << "The day after ";
        ShowDate(m, d, y);
        bool errorCode = NextDate(m, d, y);
        if (!errorCode) {
            cout << " is ";
            ShowDate(m, d, y);
        }
        else {
            cout << endl << "ERROR: date is invalid." << endl;
        }
      
        cout << endl << "Test again? (Type y for yes or n for no): ";
        cin >> ans;
        cout << endl;
    } while(ans == 'y' || ans == 'Y');
  
    return 0;
}

// GetDate function: user enter date in m/d/y format
void GetDate(int &m, int &d, int &y) {
    char slash;
    cout << "Enter the calendar date in m/d/y format: ";
    cin >> m >> slash >> d >> slash >> y;
}

// ShowDate function: display date
void ShowDate(int m, int d, int y) {
    cout << m << "/" << d << "/" << y;
}

// NextDate function

bool NextDate(int &m, int &d, int &y) {
    if (!DateIsValid(m, d, y)) {
        return true;
    }
    else {
        int days_in_month = DaysInMonth(m, y);
        if (d == days_in_month) {
            m++;
            d = 1;
        }
        else {
            d++;
        }
      
        if (m > 12) {
            y++;
            m = 1;
        }
        return false;
    }
}

// DateIsValid function: validate entered month and date

bool DateIsValid(int &m, int &d, int &y) {
    if (d > 0 && d <= DaysInMonth (m, y))
        return true;
    else if (d == 0 || m == 0)
        return false;
    else
        return false;
}

// DaysInMonth function: calculate number of days base on Month user entered

int DaysInMonth (int m, int y) {
    switch (m) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
            break;
        case 4: case 6: case 9: case 11:
            return 30;
            break;
        case 2:
            if (IsLeapYear(y))
                return 29;
            else
                return 28;
            break;
        default:
            return 0;
            break;
    }
}

// IsLeapYear function: validate leap year

// leap year
bool IsLeapYear(int y) {
    if (y % 4 == 0) {
        if (y % 100 == 0) {
            if (y % 400 == 0)
                return true;
            else
                return false;
        }
        else
            return true;
    }
    else
        return false;
}

output

Enter the calendar date in m/d/y format: 6/17/1988                                                                                                          
The day after 6/17/1988 is 6/18/1988                                                                                                                        
Test again? (Type y for yes or n for no): n                                                                                                                 
                                                                                                                                                            

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote