Question

C++ code: Days in current month: Write a program that can determine the number of days...

C++ code:

Days in current month:

Write a program that can determine the number of days in a month for a specified month and year. The program should allow a user to enter two integers responding a month and a year, and it should determine how many days are in the specified month. The integers 1 through 12 will be used to identify the months of January through December. The user indicates the end of days in the current month and terminates.

Use the following criteria to identify leap year:

1. A year Y is divisible by 100. Then Y is a leap year if and only if it is divisible by 400. For example, 2000 is a leap year but 2100 is not.

2. A year Y is not divisible by 100. Then Y is a leap year if and only if it is divisible by 4. For example, 2008 is a leap year but 2009 is not.

Here is sample run of the program:

Enter month and year: 2 2008 [Enter]

29 days

Enter month and year: 0 0[Enter]

The current month, September 2009, has 30 days.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
#include <time.h>


using namespace std;

string getMonth(int a){
   switch(a){
       case 1 : return "January";
                break;
       case 2 : return "February";
                break;
       case 3 : return "March";
                break;
       case 4 : return "April";
                break;
       case 5 : return "May";
                break;
       case 6 : return "June";
                break;
       case 7 : return "July";
                break;
       case 8 : return "August";
                break;
       case 9 : return "September";
                break;
       case 10 : return "October";
                break;
       case 11 : return "November";
                break;
       case 12 : return "December";
                break;
   }
}

int getDays(int m, int y){

   if (m == 2){
      if ((y % 100 == 0 && y % 400 == 0) || (y % 100 != 0 && y % 4 == 0)){
         return 29;
      }
      else {
        return 28;
      }
   }
   switch(m) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12: return 31;
               break;
      case 4:
      case 6:
      case 9:
      case 11: return 30;
               break;
             
     
   }
}

void getCurrentData(){

   time_t theTime = time(NULL);
   struct tm *aTime = localtime(&theTime);

   int day = aTime->tm_mday;
   int month = aTime->tm_mon + 1;
   int year = aTime->tm_year + 1900;

   int n = getDays(month,year);
   string name = getMonth(month);
   cout << "The current month, " << name << " " << year << ", has " << n << " days" << endl;
}

int main(){

   int m,y;

   cout << "Enter month and year:";
   cin >> m >> y;
   if (m == 0 && y == 0){
       getCurrentData();
   }
   else {
     int n = getDays(m,y);
     cout << n << " days" << endl;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ code: Days in current month: Write a program that can determine the number of days...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2:...

    Complete task using python code From definitions file: month = ‘February’ year = 1200 Problem 2: Leap Years (2 Points) Your task is to write a program that takes in both a month and year (defined in the definitions file) and outputs the number of days for that month. It should take into account whether the year is a leap year or not. The resulting number of days in a given month/year combo is to be assigned to the variable...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • Write a program that accepts a year and determines whether the year is a leap year....

    Write a program that accepts a year and determines whether the year is a leap year. Use the mod function. The output should be the variable extra_day, which should be 1 if the year is a leap year and 0 otherwise. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100 but not by 400 are not leap years. Years divisible by 4...

  • 3. Using no date-specific Java libraries, write a program that computes the number of days between...

    3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...

  • An ordinal date consists of a year and a day within it, both of which are...

    An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31. Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can...

  • I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!

    I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you! Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds to that date. Your program should take the input in numeric form, thus the input prompt should be as follows: Please enter a date below: Enter month (1-12): Enter day (1-31) Enter year...

  • Write a program that prompt the user to enter 3 integers: x,y and z. Your program...

    Write a program that prompt the user to enter 3 integers: x,y and z. Your program should output the answer to the user based on the divisibility of x and y by z as follows: Input If both x and y are divisible by z Result X and y are both divisible by 2. X is divisible by z. Y is divisible by z. Both X and Y are not divisible by If x is only divisible by z If...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT