Write a program that prints the day number of the year, given the date in the fo
ID: 3622240 • Letter: W
Question
Write a program that prints the day number of the year, given the date in the form month-date-year. For example, if the input is 1-1-2008, the day number is 1; if the input is 12-25-2008, the day number is 360.The program should check for a leap year:
A year is a leap year if it is divisible by 4 but not by 100. For example, the year 1996 and the year 2008 are divisible by 4 but not by 100.
A year that divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are leap years since they are divisible by 400; However, 1800 is not a leap year since it is not divisible by 400.
Your program must use user defined functions to accomplish certain tasks, for instance:
1. A void function getMonthDateYear that reads input .
2. An integer function calculateDays that calculates the day numbers for given input;
3. A Boolean function isLeapYear that returns true if the given year is a leap year; otherwise, returns false.
This is what I have so far, but it does not work:
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<fstream>
void getMonthDateYear(int&month,int&day,
int&year,char&ch);
int calculateDays(int dayNumber);
bool isLeapYear ( int year );
int main ()
{
int day, month, year, dayNumber;
char ch;
getMonthDateYear(month,day,year,ch);
calculateDays(dayNumber);
system ("PAUSE");
return 0;
}
void getMothDateYear(int&month,int&day,
int&year,char&ch);
cout << " Enter a date(mm-dd-yyyy) : " ;
cin >> month;
cin >> ch;
cin >> day;
cin >> ch;
cin >> year;
}
int calculateDays( int dayNumber );
{
dayNumber = 0;
while ( month > 1 )
{
switch( month - 1 )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
dayNumber += 31;
break;
case 4 :
case 6 :
case 9 :
case 11:
dayNumber += 30;
break;
case 2 :
dayNumber += 28;
if ( isLeapYear( year ) )
dayNumber++;
break;
}
month--;
}
dayNumber += day;
cout << " The day number is "
<< dayNumber;
return 0;
}
bool isLeapYear( int year )
{
if ((( year % 4 == 0 ) &&(year % 100 != 0 ))
||(( year % 100 == 0 ) && (year % 400 == 0 )))
return true;
return false;
}
Explanation / Answer
please rate - thanks
nice job, I corrected very little-I think I got all corrections highlighted
#include<iostream>
#include<stdlib.h>
#include<iomanip>
#include<fstream>
using namespace std;
void getMonthDateYear(int& month,int& day,int& year);
int calculateDays(int month,int day, int year);
bool isLeapYear ( int year );
int main ()
{
int day, month, year, dayNumber;
char ch;
getMonthDateYear(month,day,year);
dayNumber=calculateDays(month,day,year);
cout << " The day number is "<< dayNumber<<endl;
system ("PAUSE");
return 0;
}
void getMonthDateYear(int&month,int&day,int&year)
{char ch;
cout << " Enter a date(mm-dd-yyyy) : " ;
cin >> month;
cin >> ch;
cin >> day;
cin >> ch;
cin >> year;
}
int calculateDays( int month,int day, int year)
{
int dayNumber = 0;
while ( month > 1 )
{
switch( month - 1 )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
dayNumber += 31;
break;
case 4 :
case 6 :
case 9 :
case 11:
dayNumber += 30;
break;
case 2 :
dayNumber += 28;
if ( isLeapYear( year ) )
dayNumber++;
break;
}
month--;
}
dayNumber += day;
return dayNumber;
}
bool isLeapYear( int year )
{
if ((( year % 4 == 0 ) &&(year % 100 != 0 ))
||(( year % 100 == 0 ) && (year % 400 == 0 )))
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.