Start New Discussion within our Software Development Community

Don't know how to approach this problem, here's what I have so far, any help would be greatly appreciated.

PROBLEM : Modify the function days() written for exercise 1 to account for the actual number of days in each month. Assume each year contains 365 days( that is do not account for leap years).

#include <iostream> 
#include <string>
using namespace std;


struct Date
{
	int month;
	int day;
	int year;
};

int days(Date*);

int main()
{
	const Date date_init = {0,0,0};
	Date* dt1 = new Date();
	*dt1 = date_init;
	cout << " Please input the days" ;
	cin >> dt1->day;  
    cout << " Please input the months" ;
    cin >> dt1->month;  
    cout << " Please input the year" ;
    cin >> dt1->year; 
    days(dt1); // function that takes a structure variable as argument."; 
    cout << days(dt1);
    delete dt1;
    return 0;

}


int days(Date *d1)
{
	 int convdays;
     int basis;
	 convdays = (d1->day) - 1 + 30*((d1->month) -1) + 365*((d1->year) -1900);
	 return convdays;
}

Excepting leap years, a month could have 28, 30, or 31 days. Your Date object contains the month, so you can use that to determine how many days there are in the preceding months quite easily using either a switch or table lookup.

For example, you want to know how many days are in 5/27. January has 31 days, February has 28, March has 31, and April has 30. Add them up including the days in the current month and you get the number of days within the year:

31 + 28 + 31 + 30 + 27 = 147

This process corresponds to the (d1->day) - 1 + 30*((d1->month) -1) part of your current calculation.

This article has been dead for over six months. Start a new discussion instead.