Question: Topic Branching switch Statement Description Write a program that converts a date into its corresponding day number. The program asks the user to input any
Topic
Branching
switch Statement
Description
Write a program that converts a date into its corresponding day number. The program asks the user to input any date within the current century as three whole numbers representing month, day, and year. It computes the day number corresponding to the date entered and displays it. For example, for a month value of 3, day value of 1, and year value of 2007, it displays 60.
Details
Each day of the year has a day number associated with it from 1 to 365/366. For example, for a non-leap year the day number for January 1st is 1, for March 1st is 60 and for December 31st is 365. For a leap year, the day number for January 1st is 1, for March 1st is 61 and for December 31st it is 366.
This assignment limits the year to be within the current century i.e. from 2000 to 2099. This makes it easier to determine the leap year. Within the above range, a leap year is the year that is divisible by 4.
Testing
Input Test Run 1
Enter Month: 2
Enter Day: 29
Enter Year: 2004
Output Test Run 1
Date: 2/29/2004
Day Number: 60
Input Test Run 2
Enter Month: 3
Enter Day: 1
Enter Year: 2003
Output Test Run 2
Date: 3/1/2003
Day Number: 60
Input Test Run 3
Enter Month: 3
Enter Day: 1
Enter Year: 2004
Output Test Run 3
Date: 3/1/2004
Day Number: 61
Sample Code
/*
The sample code snippet below asks the user to input month, day and year one after the other. Then it computes the daynumber.
*/
int day, month, year;
//input month, day, and year values.
cout << "Enter month as a number" << endl;
cin >> month;
cout << "Enter day as a number" << endl;
cin >> day;
cout << "Enter year as a number" << endl;
cin >> year;
//Determining Leap year using a boolean variable
bool leapyear;
leapyear = false;
if ((year % 4) == 0)
{
leapyear = true;
}
// Determining day number
int dayNum;
switch (month) {
case 1:
dayNum = day;
break;
case 2:
dayNum = day + 31;
break;
case 3:
dayNum = day + 31 + 28;
break;
case 4:
dayNum = day + 31 + 28 + 31;
break;
/// more code for other months
}
//add 1 for a leap year
if (leapyear==true && month > 2)
dayNum ++;
//output the date and dayNum
IN C++ PLEASE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
