Write a program that asks the user to input a date in the form MM/DD/YYYY and ou
ID: 3829504 • Letter: W
Question
Write a program that asks the user to input a date in the form MM/DD/YYYY and outputs the day number (1 to 366) of that day within the year.
Write a program that asks the user to input a date in the form MM/DD/YYYY and outputs the day number (1 to 366) of that day within the year. For example: For an entered date of 01/01/1994 the output would be day 1 For an entered date of 12/31/1993 the output would be day 365. For an entered date of 12/31/1996 the output would be day 366. Display an error message when an invalid date is entered. Hint: If you use cin to read the month into an integer variable month the extraction stops at the trailing character and the next extraction will read the character Sample output of a program that satisfies the requirements of this assignment is given below. Sample output 1: Enter a date in time in the format MM/DD/YYYY: 02/11/1998 2/11/1998 is day number 42 of the year 1998Explanation / Answer
C++ code to print day number of the year, when input date format is mm/dd/year
//Code
#include <iostream>
using namespace std;
void dayNumber(int, int, int);
int main()
{
int month;
int day;
int year;
std::cin >> month;
if ( std::cin.get() != '/' )
{
std::cout << "Invalid date";
return 1;
}
std::cin >> day;
if ( std::cin.get() != '/' )
{
std::cout << "Invalid date";
return 1;
}
std::cin >> year;
dayNumber(day,month,year);
return 0;
}
void dayNumber(int day, int month, int year)
{
int dayNum = 0;
bool isLeap;
if (year % 4 == 0)
{
dayNum = 366;
isLeap = true;
}
else
{
dayNum = 365;
isLeap= false;
}
switch (month)
{
case 12:dayNum -= (31-day);
break;
case 11:dayNum -= (61-day);
break;
case 10:dayNum -= (92-day);
break;
case 9:dayNum -= (122-day);
break;
case 8:dayNum -= (153-day);
break;
case 7:dayNum -= (184-day);
break;
case 6:dayNum -= (214-day);
break;
case 5:dayNum -= (245-day);
break;
case 4:dayNum -= (275-day);
break;
case 3:dayNum -= (306-day);
break;
case 2:
{
if(isLeap)
dayNum -= (335-day);
else
dayNum -= (334-day);
}
break;
case 1:
{
if(isLeap)
dayNum -= (366-day);
else
dayNum -= (365-day);
}
break;
default:
cout<<"invalid date"<<endl;
}
cout<<dayNum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.