Answer to Question #180318 in C++ for Deepanshi singh

Question #180318

Write a program in c++ to accept data dd:mm: yyyy format store these values in my drive classes with member as day ,month, year . convert this data into one variable duration of the current year .this duration means number of days passed in the current year since jan1


1
Expert's answer
2021-04-11T08:00:35-0400
#include <bits/stdc++.h>
using namespace std;

// days in month
int daysInMonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class Date {
private:
        int day,
            month,
            year;
public:
        Date() {
                cout << "Enter date in DD:MM:YYYY format: ";
                string date;
                cin >> date;
                day = stoi(date.substr(0, 2));
                month = stoi(date.substr(3, 2));
                year = stoi(date.substr(6, 4));
        }
        Date(int d, int m, int y) {
                day = d, year = y, month = y;
        }

        void calculateDaysPassed() {
                // calculates the days passed since Jan 1

                int daysPassed = 0;
                if (leapYear(year)) daysInMonths[1]++; // feb has 29 days in leap years
                for (int i = 0; i < month - 1; i++) {
                        daysPassed += daysInMonths[i];
                }
                daysPassed += day;

                cout << "\nDays passed since Jan 1 in year " << year << " are " << daysPassed << endl;
        }

        // checks if the year is a leap year or not
        bool leapYear(int year) {
                if (((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)))) return true;
                else return false;
        }

};


int main() {
        // declare the Date object. It'll ask the user for date
        Date today;

        // this will calculate the days passed since Jan 1
        today.calculateDaysPassed();
        return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Deepanshi singh
11.04.21, 15:17

Good

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS