Question

In: Computer Science

Write a c++ program that asks the user for a date (Month and Day only) and...

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

Solutions

Expert Solution

#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');


Related Solutions

Day of week Write a program that asks the user for a date (year, then month,...
Day of week Write a program that asks the user for a date (year, then month, then day, each entered as a number on a separate line), then calculates the day of the week that date falls on (according to the Gregorian calendar) and displays the day name. You may not use any date functions built into Python or its standard library, or any other functions that do this work for you. Hint: Reverend Zeller may be useful, but his...
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month...
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month as an integer between 1 and 12. It is OK for your program to crash when the user does not enter a valid integer. Prints out the name of the corresponding month. Prints out "This number does not correspond to a month." if the integer is less than 1 or greater than 12. For example: if the user enters 1, your program output should...
In C write a program that asks the user for the dimensions of 3 sides of...
In C write a program that asks the user for the dimensions of 3 sides of a triangle. Each side dimension must be between 1.00 and 100.00 including 1.00 and 100.00. If the side dimensions indeed can make a valid triangle then the program should use these values to determine the perimeter and the area of the valid triangle 1. Must use programmer define functions 2. Your program must have at least 4 functions, main and the following 3 functions:...
c program Write a program that asks the user to enter a sequence of 15 integers,...
c program Write a program that asks the user to enter a sequence of 15 integers, each either being 0, 1, or 2, and then prints the number of times the user has entered a "2" immediately following a "1". Arrays are not allowed to appear in your code. Include ONLY THE SCREENSHOT OF YOUR CODE in an image file and submit the file.
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT