Question

Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one...

Given the poem:

Thirty days hath September
April, June, and November
All the rest have thirty-one
with February's 28 to make it fun.
Leap Year happening one in four, Gives February one day more.

How would I write this program using an Enum instead of an array?

1. Prompts the user for a month string (January, February, ...). Validate the input month string against an array of month's, and prompt the user again if the String entered is not a valid month. Note: You may want to use an Enum.

2. Prompts the user for a year (e.g. 2016).

3. For months with 31 days, output the phrase "all the rest have thirty-one". For September, the phrase "Thirty days hath September". For April, June, and November the phrase "April, June, and November". For February, if the year is not a leap year, output the phrase "with February's 28 to make it fun.". If the use did supply a leap year (and the user supplied February as the month), output the phrase "Leap Year happening one in four, Gives February one day more."

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

typedef enum months {
    JANUARY = 1,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE,
    JULY,
    AUGUST,
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER,
    INVALID
} Month;

Month getMonth(std::string input) {
    if (input.compare("January") == 0) {
        return JANUARY;
    } else if (input.compare("February") == 0) {
        return FEBRUARY;
    } else if (input.compare("March") == 0) {
        return MARCH;
    } else if (input.compare("April") == 0) {
        return APRIL;
    } else if (input.compare("May") == 0) {
        return MAY;
    } else if (input.compare("June") == 0) {
        return JUNE;
    } else if (input.compare("July") == 0) {
        return JULY;
    } else if (input.compare("August") == 0) {
        return AUGUST;
    } else if (input.compare("September") == 0) {
        return SEPTEMBER;
    } else if (input.compare("October") == 0) {
        return OCTOBER;
    } else if (input.compare("November") == 0) {
        return NOVEMBER;
    } else if (input.compare("December") == 0) {
        return DECEMBER;
    } else {
        return INVALID;
    }
}

bool isLeapYear(int year) {
    if (year %4 == 0) {
        return true;
    }
    return false;
}

int main () {
    std::string user_month = "";
    int user_year;
    Month month = INVALID;

    std::cout<<"Enter month: ";
    std::cin >> user_month;
    std::cout<<"Enter year: ";
    std::cin>>user_year;
    bool isLeap = isLeapYear(user_year);
    do { 
        month = getMonth(user_month);
    }while (month == INVALID);

    switch (month) {
        case JANUARY:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                     } break;
        case FEBRUARY:{
                          if (isLeap) {
                            std::cout<<"\nwith February's 28 to make it fun.\n";
                          } else {
                            std::cout<<"\nLeap Year happening one in four, Gives February one day more.\n";
                          }
                      } break;
        case MARCH:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                   } break;
        case APRIL:{
                         std::cout<<"\nApril, June, and November\n";
                   } break;
        case MAY:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                 } break;
        case JUNE:{
                         std::cout<<"\nApril, June, and November\n";
                  } break;
        case JULY:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                  } break;
        case AUGUST:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                    } break;
        case SEPTEMBER:{
                         std::cout<<"\nThirty days hath September.\n";
                       } break;
        case OCTOBER:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                     } break;
        case NOVEMBER:{
                         std::cout<<"\nApril, June, and November\n";
                      } break;
        case DECEMBER:{
                         std::cout<<"\nall the rest have thirty-one.\n";
                      } break;
        case INVALID: {
                          /* do nothing this is not possible */
                      } break;
    }

    return 0;
}

/* hope this helps if any queries please do comment*/

Add a comment
Know the answer?
Add Answer to:
Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one...
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
  • Javafx Given the poem: Thirty days hath September April, June, and November All the rest have...

    Javafx Given the poem: Thirty days hath September April, June, and November All the rest have thirty-one with February's 28 to make it fun. Leap Year happening one in four, Gives February one day more. Write a program that does the following: 1. Prompts the user for a month string (January, February, ...). Validate the input month string against an array of month's, and prompt the user again if the String entered is not a valid month. Note: You may...

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on

    # JAVA In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).The program reads a month and year from the user.When the year is a leap year, change the entry for February to 29.Print out how many days the given month has.CODE:import java.util.Scanner;public class NumberOfDays{   public static void main(String[] args)   {      // Declare and initialize daysOfMonth      ....

  •    Lab/HW 3   Write a program that reads in the following data, all entered on one...

       Lab/HW 3   Write a program that reads in the following data, all entered on one line with at least one space separating them: a) an integer representing a month b) an integer representing the day of the month c) an integer representing a year 1. Check that the month is between 1 and 12. If it’s not print an error message. 2. Day cannot be 0 or less nor can it be more than 31 for all months. Print...

  • Cash Discount Review, Tu 41400 Chapter 8 and Distribution Finance Handy Rule: "30 days hath September. April June...

    Cash Discount Review, Tu 41400 Chapter 8 and Distribution Finance Handy Rule: "30 days hath September. April June and November all the others have 31 expect February which has 28 and sometimes 29" (author unknown) 1. Calculate the cash discount offered and the amount paid as stated in the problems below: Item Invoice Amount Invoice Date Discount terms Date Pald Amount Pald a $1,790 1/10, n/30 3/1 3/10 $1,772.10 b. $7,194 2/10, n/30 3/28 4/17 $7,050 2. A distributor is...

  • Occupancy-Days 3,400 280 Nm 0 0 Month January February March April May June July August September...

    Occupancy-Days 3,400 280 Nm 0 0 Month January February March April May June July August September October November December Electrical Costs $ 10,234 $10,052 $11,605 $ 6,825 $ 2,380 $ 6,860 $ 11,060 $11,100 $ 7,602 $ 3,605 $ 4,634 $ 8,974 4,040 4,080 2,240 1,030 1,460 2,700 Required: 1. Using the high-low method, estimate the fixed cost of electricity per month and the variable cost of electricity per occupancy-day. (Do not round your intermediate calculations. Round your Variable cost...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Write the Flowchart for the following programming problem based on the pseudocode below. Last year, a...

    Write the Flowchart for the following programming problem based on the pseudocode below. Last year, a local college implemented rooftop gardens as a way to promote energy efficiency and save money. Write a program that will allow the user to enter the energy bills from January to December for the year prior to going green. Next, allow the user to enter the energy bills from January to December of the past year after going green. The program should calculate the...

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