The program in dayOfYearcpp works but it is ugly and harder

The program in dayOfYear.cpp works, but it is ugly and harder to understand than it should be. That’s because it was written without functions other than main. As a consequence, a lot of code gets repeated, and the overall logic of the program is hidden by the mass of details.

Rewrite the program by grouping the calculations into functions. In particular, you must introduce the following functions:

A function named numberOfDaysInMonth that returns an int containing the number of days in a month, given the month and year, (passed, in that order, as int parameters).

A function named dateIsValid that tests a date to see if it is a valid date in the Gregorian calendar, returning true if it is valid and false if it is not. The inputs are the year, month, and day (passed, in that order, as int parameters).

A function isALeapYear that takes a year (int) as a parameter and determines whether a given year is a leap year or not, returning true or false as appropriate

A function named dayOfTheYear to compute the day number within the year. This function will have no return value. Its parameters, all integers, will be, in order, the year, month, and day (inputs) and the day number (output).

As you introduce each function, replace the code in main() by calls to your new functions as appropriate. In particular, note that some of these new functions may be called from within the bodies of some of the other functions.

Remember that this program was already working. You should not alter the input or output of the program, as it would be observed by someone running the program, in any way.

dayOfYear.cpp

Solution

Hi

I have modified the code as per your requirement. Highlighted the code changes below

#include <iostream>
#include <iomanip>

using namespace std;

bool isALeapYear(int year){
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
}
else{
return false;
}
}
int numberOfDaysInMonth(int month, int year){
int numberOfDaysInMonth = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numberOfDaysInMonth = 31;
break;

case 4:
case 6:
case 9:
case 11:
numberOfDaysInMonth = 30;
break;

case 2:
if (isALeapYear(year)) {
numberOfDaysInMonth = 29;
}
else {
numberOfDaysInMonth = 28;
}
}
return numberOfDaysInMonth;
}

bool dateIsValid (int year, int month, int day){
// Check to see if this is a valid date

// The Gregorian calendar began On Oct 15, 1582. Earlier dates
// are invalid.
if (year < 1582) {
return false;
}
else if (year == 1582 && month < 10) {
return false;
}
else if (year == 1582 && month == 10 && day < 15) {
return false;
}
// Months must be in the range 1..12
else if (month < 1 || month > 12) {
return false;
}
// Days must be in the range 1..K where K is the number of
// days in that month.
else {
  
int days = numberOfDaysInMonth(month, year);
if (day < 1 || day > days) {
return false;
}
else {
return true;
}
}

  
}
int dayOfTheYear (int year, int month){
int sum = 0;
for (int m = 1; m < month; ++m) {
int monthLength = 0;
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthLength = 31;
break;

case 4:
case 6:
case 9:
case 11:
monthLength = 30;
break;

case 2:
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
monthLength = 29;
}
else{
monthLength = 28;
}
}

sum += monthLength;
  
}
return sum;

  
}
int main() {
int year, month, day;
char c;
bool validInput = false;

while ((cin) && !validInput) {
cout << \"Enter a date in the form YYYY-MM-DD: \" << flush;
cin >> year >> c >> month >> c >> day;
validInput = dateIsValid (year, month, day);
if (!validInput) {
cout << \"Sorry, that is not a valid date\" << endl;
string garbage;
getline (cin, garbage); // discard the rest ofthe input line
}
}
if (!cin) {
cout << \"Could not obtain valid input.\" << endl;
return -1;
}

// Input is valid - compute the day number
int sum = dayOfTheYear ( year, month);
// Add up the number of days in all earlier months
// of this year
// Then add the day number to that sum
int dayNum = sum + day;

cout << setw(2) << setfill(\'0\') << month
<< \"/\" << setw(2) << setfill(\'0\') << day << \"/\"
<< setw(4) << year;
cout << \" is day #\" << dayNum << \" of that year.\" << endl;

return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a date in the form YYYY-MM-DD: 2012-02-28                                                                                                                                                                                                                        

02/28/2012 is day #59 of that year.

The program in dayOfYear.cpp works, but it is ugly and harder to understand than it should be. That’s because it was written without functions other than main.
The program in dayOfYear.cpp works, but it is ugly and harder to understand than it should be. That’s because it was written without functions other than main.
The program in dayOfYear.cpp works, but it is ugly and harder to understand than it should be. That’s because it was written without functions other than main.
The program in dayOfYear.cpp works, but it is ugly and harder to understand than it should be. That’s because it was written without functions other than main.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site