Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to separate a program into functions. I keep getting errors that I d

ID: 3674635 • Letter: I

Question

I am trying to separate a program into functions. I keep getting errors that I don`t understand. The first error is the bracket right after void dayOfTheYear(). It says "A function definition is not alowed here before '{' token. The second error is the very end bracket. It says "Expected '}' at the end of input. Please tell me where my mistakes are.


#include <iostream>
#include <iomanip>

using namespace std;

int numberOfDaysInMonth();
bool dateIsValid();
bool isALeapYear();
int dayOfTheYear();

int main()
{
int year, month, day;
int sum;
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;
}
bool dateIsValid();
int numberOfDaysInMonth();
int dayOfTheYear();
bool isALeapYear();
// 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;
}

//-------------------------------------------------------------------
// Check to see if this is a valid date
// The Gregorian calendar began On Oct 15, 1582. Earlier dates
// are invalid.
//-------------------------------------------------------------------
bool dateIsValid()
{
int year;
bool validInput;
int month;
int day;
if (year < 1582)
   validInput= false;
else if (year == 1582 && month < 10)
   validInput = false;
else if (year == 1582 && month == 10 && day < 15)
   validInput = false;
// Months must be in the range 1..12
else if (month < 1 || month > 12)
   validInput = false;
}


//--------------------------------------------------------------------
// Days must be in the range 1..K where K is the number of
// days in that month.
//--------------------------------------------------------------------
int numberOfDaysInMonth()
{
int month;
int day;
bool validInput;
   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() == true)
{
numberOfDaysInMonth = 29;
}
else
{
numberOfDaysInMonth = 28;
   }

if (day < 1 || day > numberOfDaysInMonth)
   validInput = false;
else
   validInput = true;

if (!validInput)
   {
   cout << "Sorry, that is not a valid date" << endl;
   string garbage;
   getline (cin, garbage); // discard the rest of the input line
   }
if (!cin)
{
cout << "Could not obtain valid input." << endl;
return -1;
}
}

//--------------------------------------------------------
// Input is valid - compute the day number
// Add up the number of days in all earlier months
// of this year
//--------------------------------------------------------
int dayOfTheYear()
{
for (int m = 1; m < month; ++m)
{
int monthLength = 0;
int sum = 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 (isALeapYear() == true)
{
monthLength = 29;
}
else
{
monthLength = 28;
}

sum += monthLength;
}
}
}

//------------------------------------------------------
// Perform calculations to see whether this is a
// leap year and return true or false
//------------------------------------------------------
bool isALeapYear(int)
{
int year;
if (((year % 4 == 0) && (year % 100 != 0))
       || (year % 400 == 0))
{
return true;
}
else
{
return false;
}
}

Explanation / Answer

You are not closed numberOfDaysInMonth() method under switch case.bellow code numberOfDaysInMonth() method code replace in your code

//--------------------------------------------------------------------
// Days must be in the range 1..K where K is the number of
// days in that month.
//--------------------------------------------------------------------
int numberOfDaysInMonth()
{
int month;
int day;
bool validInput;
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() == true)
{
numberOfDaysInMonth = 29;
}
else
{
numberOfDaysInMonth = 28;
}
if (day < 1 || day > numberOfDaysInMonth)
validInput = false;
else
validInput = true;
if (!validInput)
{
cout << "Sorry, that is not a valid date" << endl;
string garbage;
getline (cin, garbage); // discard the rest of the input line
}
if (!cin)
{
cout << "Could not obtain valid input." << endl;
return -1;
}
   }  
}
//--------------------------------------------------------
// Input is valid - compute the day number
// Add up the number of days in all earlier months
// of this year
//--------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote