bool IsValidYear( int );
bool IsValidMonth( int );
bool IsValidDay( int, int, int );
bool IsLeapYear( int );
using namespace std ;
int main()
{
int year = 0 ;
int month = 0 ;
int day = 0 ;
bool inputIsValid = false;
do
{
cout << "\nEnter a year: " ;
cin >> year ;
inputIsValid = IsValidYear(year);
if ( !inputIsValid)
cout << "\n\n" << year << " is not a valid year. Try again.\n";
} while (!inputIsValid);
// Write a loop like the one for obtaining the year, and
// use IsValidMonth to determine whether a month is valid.
cout << "\nEnter a month: " ;
cin >> month ;
inputIsValid = IsValidMonth(month);
if ( !inputIsValid)
cout << "\n\n" << month << " is not a valid month. Try again.\n";
// Write a loop like the one for obtaining the year, and
// use IsValidDay to determine whether a day is valid.
cout << "\nEnter a day: " ;
cin >> day;
inputIsValid = IsValidDay(day);
if ( !inputIsValid)
cout << "\n\n" << day << " is not a valid day. Try again.\n";
cout << "\n\n" << month << "/" << day << "/" << year << " is a valid date.\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
// Function definitions:
bool IsValidYear( int year )
{
if ( year >= 0 ) return true;
else return false;
}
bool IsValidMonth( int month)
{
if (month >= 1 && month <= 12)
return true;
else return false;
}
bool IsValidDay( int year, int month, int day)
{
// Complete this function.
switch (month)
{
case 4:
return 30;
case 6:
return 30;
case 9:
return 30;
case 11:
return 30;
// The valid range of days for these months is 1 to 30.
case 2:
if ((year%4==0) && (year %100!= 0) || (year%400 == 0))
{
// If this is a leap year, the valid range of days for this month is 1 to 29.
return 29;
}
// If this is NOT a leap year, the valid range of days for this month is 1 to 28.
else return 28;
default:
return 31;
// The valid range of days for these months is 1 to 31.
}
}
bool IsLeapYear( int year)
{
if ((year%4==0) && (year %100!= 0) || (year%400 == 0))
{
return true;
}
else
{
return false;
}
// Complete this function using the following rules:
// If a year is evenly divisible by 400, it is a leap year.
// If a year is evenly divisible by 4 and NOT evenly divisible by 100, it is a leap year.
// Otherwise, it is not a leap year.
I think the compile error are quite obvious to fix:
1 2 3 4 5
test.cpp: In function ‘int main()’:
test.cpp:8: error: too few arguments to function ‘bool IsValidDay(int, int, int)’
test.cpp:49: error: at this point in file
test.cpp:58: error: ‘system’ was not declared in this scope
test.cpp:59: error: ‘EXIT_SUCCESS’ was not declared in this scope
- You are calling IsValidDay with only one argument on line 49, but the function requires 3.
- system() is declared in stdlib.h
- EXIT_SUCCESS is declared in stdlib.h