Answer to Question #180287 in C++ for Manisha gupta

Question #180287

Write a program in C++ to accept date in dd:mm:yyyy format. Store these values in MyDate Class with members as day, month and year. Convert this date into one variable “Duration” of the current years. This duration means number of days passed in the current year since Jan 1.

Hint :Use type conversion method from class type to basic type conversions.

Note: consider days in February as 28 for all years.

Output expected:

Enter Date : 05:04:2021

No of days since Jan 1 of the current year : 90


1
Expert's answer
2021-04-13T14:32:29-0400
#include<bits/stdc++.h>
using namespace std;

// List of days in a month added continuosly
int a[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

class MyDate{
        int day, month, year, duration;

public:
        void setValues(int dd, int mm, int yyyy) {
                day = dd;
                month = mm;
                year = yyyy;
                calculateDuration();
        }
        
        int getDuration() {   
                return duration;
        }
        
        void calculateDuration() {
                // As per given test case (if not considering input date, 
                // for ex. in test case 05 days were not added)
                duration = a[month-1];
                // If you want to consider input date as well, then
                // use following line - uncomment it
                // duration = a[month-1] + day;
        }
};

int main() {
        string s;
        cout << "Enter Date: ";
        cin >> s;
        
        // Preprocessing of input string
        int d = ((s[0]-'0') * 10) + (s[1]-'0');
        int m = ((s[3]-'0') * 10) + (s[4]-'0');
        int y = ((s[6]-'0') * 1000) + ((s[7]-'0') * 100) + ((s[8]-'0') * 10) + (s[9]-'0');
        
        // Creating object and seting values
        MyDate o;
        o.setValues(d,m,y);
        cout << "No of days since Jan 1 of the current year : " << o.getDuration() << "\n";
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS