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

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 =...
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:...
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...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
Coding in C++: For this verse, you need to write a program that asks the user...
Coding in C++: For this verse, you need to write a program that asks the user to input the inventory id number (integer) and the price (float) for 5 inventory items. Once the 5 inventory items are input from the user, output the results to the screen. Please ensure you use meaningful names for your variables. If your variables are not named meaningfully, points will be deducted.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT