Create a program that counts the number of elapsed days from Jan 1, 2009 to a da
ID: 3620981 • Letter: C
Question
Create a program that counts the number of elapsed days from Jan 1, 2009 to a date in the year 2009 specified by the user. Program output should be similar to the following:This program calculates the number of days from Jan 1, 2009 to a date in the year 2009 that you specify.
Please enter a month (1 – 12): 2
Please enter a day of the month: 27
57 days elapse between January 1, 2009 and February 27, 2009.
The program should recognize invalid dates and should prompt the user to try again until they make a valid entry:
This program calculates the number of days from Jan 1, 2009 to a date in the year 2009 that you specify.
Please enter a month (1 – 12): 13
13 is not a valid entry for the month. Please enter a value between 1 and 12: 2
Please enter a day of the month: 31
31 is not a valid day in the month of February.
Please enter a value between 1 and 28: 26
56 days elapse between January 1, 2009 and February 26, 2009.
The program should work for any valid day and month in the year 2009.
Hint: Use an array to store the number of days in each month.
In addition to these labs, also review the Word document “Session 6” that has been posted to the website. Please refer to our textbook, chapter “Lesson 8” for additional information.
Explanation / Answer
I'll let you worry about the formatting. I'll just talk you through the logic.
February of 2009 was not a leap year so it's safe to assume feb. has 28 days.
int main(){
int total_days;
//Get month from user
int month;
cin >> month;
//Get day from user
int day;
cin >> day;
//Set up 12 if statements or case statements
if(month == 1)
total_days = day - 1; //If user enters January 2nd, only 1 day has passed
if(month == 2)
total_days = 31 + day - 1; //If Feb 28th is selected it adds 31 days from Jan. plus what has passed in Feb. up to the day the user entered.
if(month == 3)
total_days = 31 + 28 + days - 1; //Adds all the days from the first 2 months plus whats passed already in march.
And you just follow that pattern for all 12 months. Be sure to watch for months with 30 and 31 days in it. You can take the - 1 out of every equation but it won't tell you how many days have PASSED. It would tell you what numbered day it is. For example you could never have 365 days passed with the -1 because on Dec. 31st only 364 days have technically passed since Jan. 1st.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.