In: Computer Science
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring starts in March 21st•Summer starts in June 21st
#include<iostream>
using namespace std;
int main()
{
int date,month,flag=0;
char ch='y';
do
{
cout<<"enter data: ";
cin>>date;
cout<<"enter month: ";
cin>>month;
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
if(date>=1 && date<=31)
{
flag=0;
}
else
flag=1;
}
else if(month==4 || month==6 || month==9 || month==11)
{
if(date>=1 && date<=30)
{
flag=0;
}
else
flag=1;
}
else if(month==2)
{
if(date>=1 && date<=28)
{
flag=0;
}
else
flag=1;
}
else if(month>=1 && month<=12)
{
flag=0;
}
else
{
flag=1;
}
cout<<endl;
if(flag==0)
{
if((date>=21 && month==9) ||(month==10) || (month==11) || (date<21 && month==12))
{
cout<<" It is fall season!";
}
else if((date>=21 && month==12) || (month==1) || (month==2) || (date<21 && month==3))
{
cout<<" It is winter season!";
}
else if((date>=21 && month==3) || (month==4) || (month==5) || (date<21 && month==6))
{
cout<<"It is spring season!";
}
else if((date>=21 && month==6) || (month==7) || (month==8) || (date<21 && month==9))
{
cout<<"It is summer season!";
}
}
else if(flag==1)
{
cout<<"INVALID DATE!!"<<endl;
}
cout<<endl;
cout<<"Want to enter more ? (Press Y or y if you want to enter more)"<<endl;
cin>>ch;
}while(ch=='y' || ch=='Y');
return 0;
}
Explanation of code
1. First we have made 3 integer variables month, date and flag.
int date,month,flag=0;
2. Then we have made a character variable ch which is initialized to 'y'.
char ch='y';
3. Then we have started a do-while loop because we want user to enter data and month value atleast once.
do
{
----
}while(--);
4. Then we ask user to enter date and month.
cout<<"enter data: ";
cin>>date;
cout<<"enter month: ";
cin>>month;
5. First we are checking if the date entered by user is valid or not.
6. If month is 1,3,5,7,8,10,12 (Jan,Mar,May,Jly,Aug,Oct,Dec) then the date value should be between 1 and 31 inclusive. If user enters date outside this range then flag =1 otherwise flag=0.
if(month==1 || month==3 || month==5 || month==7 ||
month==8 || month==10 || month==12)
{
if(date>=1 &&
date<=31)
{
flag=0;
}
else
flag=1;
}
7. If month is 4,6,9,11 (Apr,Jun,Sept,Nov) then the date value should be between 1 and 30 inclusive. If user enters date outside this range then flag =1 otherwise flag=0.
else if(month==4 || month==6 || month==9 ||
month==11)
{
if(date>=1
&& date<=30)
{
flag=0;
}
else
flag=1;
}
8. If month is 2(Feb) then the date value should be between 1 and 28(considering it is a non-leap year) inclusive. If user enters date outside this range then flag =1 otherwise flag=0.
else if(month==2)
{
if(date>=1
&& date<=28)
{
flag=0;
}
else
flag=1;
}
9. Then we are checking if user enters a month value which does not lie between 1 and 12 then flag=1 otherwise flag=0.
else if(month>=1 && month<=12)
{
flag=0;
}
else
{
flag=1;
}
10. Then we have used if condition that if flag==0 that means the date entered by user is correct. Now we can check for season.
if(flag==0)
{
----
}
11. Inside this if condition we are checking if user enters a date greater than 21 of 9(sept) or a date of october or a date of november or a date less than 21st of 12(dec) then we print it is Fall season.
if((date>=21 && month==9) ||(month==10)
|| (month==11) || (date<21 && month==12))
{
cout<<" It is fall season!";
}
12. If user enters a date greater than 21 of 12(dec) or a date of january or a date of february or a date less than 21st of 3(march) then we print it is Winter season.
else if((date>=21 && month==12) ||
(month==1) || (month==2) || (date<21 && month==3))
{
cout<<" It
is winter season!";
}
13. If user enters a date greater than 21 of 3(march) or a date of April or a date of May or a date less than 21st of 6(June) then we print it is Spring season.
else if((date>=21 && month==3) ||
(month==4) || (month==5) || (date<21 && month==6))
{
cout<<"It is
spring season!";
}
14. If user enters a date greater than 21 of 6(June) or a date of July or a date of August or a date less than 21st of 9(September) then we print it is Summer season.
else if((date>=21 && month==6) ||
(month==7) || (month==8) || (date<21 && month==9))
{
cout<<"It
is summer season!";
}
15. Then we come out of the if block for flag==0.
16. Then we have a else block for flag==1 which will print INVALID if the date was not correct.
else if(flag==1)
{
cout<<"INVALID DATE!!"<<endl;
}
17. Then we have printed a message for user if he/she wants to enter more then press 'y' or 'Y'.
cout<<"Want to enter more ? (Press Y or y if you want to enter more)"<<endl;
18. Then user enters ch.
cin>>ch;
19. Then the do-while loop closes with a test condition that if user entered ch=='y' or ch=='Y' then continue execution of this do-while loop again else exit.
while(ch=='y' || ch=='Y');