Write a program that determines the day number (1 to 366) in a year for a date that is provided as input in the format dd-mm-yyyy. As an example, 01-01-2000 is day 1. 31-12-2001 is day 365. 31-12-2000 is day 366 because 2000 is the leap year. A year is a leap year, if it is divisible by 4. You may assume that user has entered input in correct format and that is a valid gate.

``````#include <iostream>

using namespace std;

void main ()
{
string astring;
int b;

cout<<"enter the date in format dd-mm--yyyy";
cin>>astring;

if (astring.at(2)==0) && (astring.at(3)==1)
b=astring.at(0)*10+astring.at(1)
else
b=astring.at(0)*10+astring.at(1)``````

thanks.

3
Contributors
4
Replies
5
Views
10 Years
Discussion Span

First you will need an int array that contains the cumulative number of days in each month. For example: `int days[] = {0, 31, 59, ... };` Next chop up the string into its individual int elements.

Then determine if year is a leap year, if so then add one to days[2] (which is Feb) and each of the other days elements beyond 2.

Now for the actual calculation.
1. Find the number of days from 1 Jan to the start of the specified month. To do that subtract 1 from the month and then get the number of days in that month from the days[] array. Example: January = 1 - 1 and days[0] = 0, Feb = 2 - 1 and days[1] = 31 because there are 31 days between 1 January and 1 February.

2. Finally just add the spefied day to the above total.

If I enter a the date 1 Feb 2008 then first get days from 1 Jan to, but not including, 1 Feb, which is days[1]. If I enter 1 March 2008 then its days[2] = 59 (or 60 if a leap year).

If I enter 17 March 2008 then its days[2] = 59 + 17 = 76.

First you will need an int array that contains the cumulative number of days in each month. For example: `int days[] = {0, 31, 59, ... };` Next chop up the string into its individual int elements.

Then determine if year is a leap year, if so then add one to days[2] (which is Feb) and each of the other days elements beyond 2.

Now for the actual calculation.
1. Find the number of days from 1 Jan to the start of the specified month. To do that subtract 1 from the month and then get the number of days in that month from the days[] array. Example: January = 1 - 1 and days[0] = 0, Feb = 2 - 1 and days[1] = 31 because there are 31 days between 1 January and 1 February.

2. Finally just add the spefied day to the above total.

If I enter a the date 1 Feb 2008 then first get days from 1 Jan to, but not including, 1 Feb, which is days[1]. If I enter 1 March 2008 then its days[2] = 59 (or 60 if a leap year).

If I enter 17 March 2008 then its days[2] = 59 + 17 = 76.

thanks but can you please send me the edited code.

>>thanks but can you please send me the edited code.
Nope, try it and post what you have done so solve this yourself.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.