HW 5
The Program
Your program should ask the user to enter:
- a month (
1
-12
representing January-December, respectively) and - a year
Your program should then output how many days are in that month. The main challenge of this program is to correctly output the days in February based on the year (leap-years). The rules for whether a year is a leap-year are given as:
There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400. The second part of the rule affects century years. For example; the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not. (source https://www.wwu.edu/skywise/leapyear.html )
Examples:
- The input:
2 2016
should output29
- The input:
1 2017
should output31
- The input:
11 2017
should output30
Using Functions
To help decompose the problem, the skeleton we provide has a blank function defined that you will need to fill in:
bool isLeapYear(int year);
Think of this as just a component of your overall program that should return true
or false
depending on whether year
is truly a leap year or not.
By solving that small task separately, it should make writing the overall program easier. You must write this function and then call it in main()
as part of your overall solution.
Idioms Used
Study and consider how to use some or all of the following idioms:
Hint: Think about the order to assign your variables and how to swap variables.
Other Requirements
- You can prompt the user however you like (with any message) but your output should be on a separate line and be the last line of output. Our auto-grader/checker looks at the last line you produce from your program and expects to find the number of days on that line.
- The user should type in the month and year with a space separating them. You may assume the user will do this.
- If the user types in a month outside of the range
1
-12
you should output-1
. - You should indent your code properly (i.e. one additional indentation level for the code inside each new set of
{...}
) and add some basic comments as to certain sections of code are trying to do at a high level. This commenting and indenting will be worth a few points on the assignment.
Please post any questions about using Vocareum on EdStem.
Writing Your Code Using Vocareum (Recommended)
This assignment will be submitted through Vocareum.
When you start the assignment in Vocareum you should see a file months.cpp
in the work
area in the left hand window pane. Click months.cpp
to open the file in the editor. You should see some comments in the editor. Enter your name and email in the indicated lines. From there you can read the other comments and then either delete them or simply leave them and start your program code beneath the comments.
Write your program (it will save automatically when you click outside of the editor window). When you’re ready to test it, you can:
- Click the
Build
button to compile it. Any compile errors will appear in the command/output (aka “terminal”) window at the bottom. You can read the errors to try to understand what the error is and what line it appears on. - Click the
Run
button to execute your program. You’ll see any output generated by your program in the “terminal” window at the bottom and can also type in input in that same window to test your code.
Repeat the build and run process until you believe your code works. Then click the Submit
button. This will submit your code. You are allowed to submit as many times as you like. When you click Submit
, our scripts will automatically run a few (not all) of our grading test cases through your program and report the results. Look at those results to ensure your code is passing these basic tests. If everything passes, you are done! Your code is submitted. If something fails, you can simply go back edit, build, run, and submit again (as much as you like).
If you have submitted on Vocareum at this point you are done! The instructions below are only if you want to use some other editor/compiler to write your code.
Writing Your Code Using Other (Non-Vocareum) Alternatives (Not Recommended)
You can write your program using any editor and compiler (if you have one installed on your machine) but will eventually need to upload your code in a file named months.cpp
to Vocareum. In addition, you may also write and test your code on an online editor/compiler such as:
cpp.sh Website
- You can enter your program in the editor window.
- When you think your code is complete, click on
Run
. There may be compile errors so look at the indicated lines (or nearby lines) and try to fix your code. ClickRun
after you make any changes to re-compile. - If your program compiles successfully, you should see the
execution
tab highlighted on the bottom of the window and see the output of your program. You can then type input values and hit enter to test your program. - If your program does not give the output you expect go back and edit your code and try again (clicking
Run
). You can rerun the program a few times, typing in different inputs to test your program.
CodingGround Website
- You can enter your program in the editor window.
- When you think your code is complete, click on the
stdin
tab next tomain.cpp
and type in the input to your program that you want to test (this is what will be fed tocin
). So for example you’d type2 2016
for February of 2016. - Then click the
Execute
button/tab and it will compile and run your program using the input you typed in thestdin
area. The results should appear on the right. If it failed to compile, you will see compiler errors and you’ll need to fix them. If it compiled correctly you will see the output of your program. Again, you need not enter input at this point since you’ve already done so earlier in thestdin
tab. - If your program does not give the output you expect go back and edit your code and try again (clicking
Execute
). You can change the input in thestdin
area to test several cases.
Leave your browser window open with your code since it is not permanently saved anywhere.
When you believe your program is finished leave that browser tab/window open and in a new tab/window, login to Vocareum, start the HW5 assignment (LeapYear Part), and in the upper-left of the resulting window, click New
..File
. A textbox will appear where you can type in the filename (leave the work/
portion there and) just type months.cpp
. In the left window pane you should now see months.cpp
appear (it must be named months.cpp
) and you can click on it to open it. A blank window should appear on the right. Cut/paste your code from your other tab/window to this window and you can click Submit
. When you click submit it should run a few automated tests to let you know if it compiled and passed some basic sanity checks. Look at the output to ensure things are working. If not, you’ll need to modify your code in Vocareum or back in your other tab/window and then cut/paste back to Vocareum.
Be sure you click submit otherwise your code will just be saved, but not submitted!!