Write a program that reads a date (day, month, year) and produces the next date.
ID: 3627023 • Letter: W
Question
Write a program that reads a date (day, month, year) and produces the next date.
The program will prompt the user for the date. The program will make sure the user eventually
enters legal data. The program computes the next date for as many dates as given until the user
enter -1 to indicate no more dates to be entered.
Note: if -1 is entered during the gathering of a date, the program should not terminate.
Example of input:(day, month, year)
23 6 2011
28 2 2011
28 2 2012
31 12 2011
-1
Example of output:
6/24/2011
3/1/2011
29/2/2011
1 1 2012
-1
Explanation / Answer
please rate - thanks
message me if any problems
#include <iostream>
using namespace std;
bool leapyear(int);
bool validdate (int day, int month, int year);
int main()
{
int day,month,year;
int i,num,a;
cout<<"enter date as (day, month, year)day = -1 to exit: ";
cin>>day;
while(day!=-1)
{ cin>>month;
cin>>year;
if (validdate(day,month,year))
{day++;
if(!validdate(day,month,year))
{month++;
if(month>12)
{month=1;
year++;
}
day=1;
}
cout<<"next day-"<<month<<"/"<<day<<"/"<<year<<endl;
}
else
cout<<month<<"/"<<day<<"/"<<year<<" is not a valid date ";
cout<<"enter date as (day, month, year)day = -1 to exit: ";
cin>>day;
}
system("pause");
return 0;
}
bool leapyear(int year)
{
return ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)));
}
bool validdate(int day, int month, int year)
{
if (day <= 0)
return 0;
switch(month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: if (day > 31) return false; else return true;
case 4 :
case 6 :
case 9 :
case 11: if (day > 30) return false ; else return true;
case 2 : if (day > 29) return false;
if (day < 29) return true;
if (leapyear(year)) return true;
else return false;
}
return 0;
}