In: Computer Science
in C++: In date calculations an important consideration is whether or not the year is a leap year. That extra day that we tack onto February is determined by a set of criteria that can be modeled using the mod function. These are • All years evenly divisible by 400 are leap years • Years evenly divisible by 100 but not by 400 are not leap years • Years divisible by 4 but not by 100 are leap years • All other years are not leap years. Write a program that prompts the user to enter an integer for year. The program will then determine if the year is a leap year or not. It will then print the year and whether or not it is a leap year. Possible outputs might be The year 2019 is not a leap year The year 1848 is a leap year. You will need an integer variable for year, another for remainder, and a boolean variable for isLeapYear For each of the four tests the calculation is a remainder calculation. For example for the remainder when dividing by four remainder = year%4;
Here is what I have so far, any help would be greatly appreciated!
// Pound includes
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
// Function Prototypes
void printHeader(string, string);
int getData(string, int, int);
bool isLeapYear(int);
void printResults(int, bool);
int main(void) {
// call printHeader to print the splash
screen
printHeader("Date", "Description");
// Declare a variable for year. Call it
year. This must be an int
int year;
// Declare a bool variable for if the year
is a leap year. Call it leap
bool leap;
// Call getData and have it return year -
this is done for you
year = getData("Enter the year: ", 1582,
100000);
// Call the function that determines if
the year is a leap year
leap = isLeapYear(year);
// Print the results using the
printResults function.
printResults(year, leap);
return 0;
}
bool isLeapYear(int);
// ISLEAPYEAR isLeapYear(int y) determines
if the integer year that was passed to
// to the function is a leap year
// Create a boolean variable to store
if the year is a leap year
bool isLeap = 0;
// Use a multiway if - else if to
determine if the year is a leap year.
// Start with the outermost criteria -
divisible by 400. Each time you will
// calculate the remainder. For example r
= y % 400. Then if(r == 0) it is
// leap year. Then do it for divisible by
100, then 4
if (year%400==0) {
isLeap = 1;
}
else if(year%100==0) {
isLeap = 0;
}
else if(year%4==0) {
isLeap = 1;
}
else {
isLeap = 0;
}
return isLeap;
}
void printResults(int year, bool isLeap) {
// PRINTRESULTS printResults(int year, bool isLeap) is
wrapper function
// that contains the cout statements used to print the
results. All printing
// should be formatted.
// Use an if - else structure for the
printing. If isLeap is true, or 1, then it
// prints the year is a leap year. If it
is not (else) then it prints that it
// is not a leap year.
return;
}
// Function Definitions
void printHeader(string dueDate, string description) {
// PRINTHEADER void printHeader(string, string) prints
the splash screen
// using the two strings that are passed to the
function from the driver.
// Print the splash screen
cout << endl;
cout << "Name" << endl;
cout << "Class" << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
int getData(string prompt, int minVal, int maxVal) {
// GETDATA getData(string prompt) is wrapper function
that contains the
// cout and cin statements used to enter data into the
program. The string
// object prompt should be used in cout to print the
prompt to the user.
int n;
// Prompt the user with the string in the
object prompt
cout << "Enter the year: ";
// Use cin to have the user enter a
value
cin >> n;
// Add a simple if to check that the
value. If it is less than minVal or
// greater than maxVal then the true block
runs. The true block prints
// a message saying that the value is
outside of the range and then calls
// the exit(13) function to quit the
program
if ((n < minVal) || (n > maxVal)) {
cout << "Value, " << n << ", is outside of the
range " << minval << " to " << maxVal <<
endl;
exit(13);
}
return n;
}
It seems that you only need the printResults function. Please do let me know if I missed anything.
I copied your code and executed it. There were a few errors that have been corrected and below is the code:
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
// Function Prototypes
void printHeader(string, string);
int getData(string, int, int);
bool isLeapYear(int);
void printResults(int, bool);
int main(void) {
// call printHeader to print the splash screen
printHeader("Date", "Description");
// Declare a variable for year. Call it year. This must be an int
int year;
// Declare a bool variable for if the year is a leap year. Call it leap
bool leap;
// Call getData and have it return year - this is done for you
year = getData("Enter the year: ", 1582, 100000);
// Call the function that determines if the year is a leap year
leap = isLeapYear(year);
// Print the results using the printResults function.
printResults(year, leap);
return 0;
}
bool isLeapYear(int year){
bool isLeap = 0;
if (year%400==0) {
isLeap = 1;
}
else if(year%100==0) {
isLeap = 0;
}
else if(year%4==0) {
isLeap = 1;
}
else {
isLeap = 0;
}
return isLeap;
}
void printResults(int year, bool isLeap) {
// PRINTRESULTS printResults(int year, bool isLeap) is wrapper function
// that contains the cout statements used to print the results. All printing
// should be formatted.
// Use an if - else structure for the printing. If isLeap is true, or 1, then it
// prints the year is a leap year. If it is not (else) then it prints that it
// is not a leap year.
if(isLeap)
cout<<"The year "<<year<<" is a leap year."<<endl;
else
cout<<"The year "<<year<<" is not a leap year."<<endl;
cout<<endl;
return;
}
// Function Definitions
void printHeader(string dueDate, string description) {
// PRINTHEADER void printHeader(string, string) prints the splash screen
// using the two strings that are passed to the function from the driver.
// Print the splash screen
cout << endl;
cout << "Name" << endl;
cout << "Class" << endl;
cout << dueDate << endl;
cout << description << endl;
cout << endl;
return;
}
int getData(string prompt, int minVal, int maxVal) {
// GETDATA getData(string prompt) is wrapper function that contains the
// cout and cin statements used to enter data into the program. The string
// object prompt should be used in cout to print the prompt to the user.
int n;
// Prompt the user with the string in the object prompt
cout << "Enter the year: ";
// Use cin to have the user enter a value
cin >> n;
// Add a simple if to check that the value. If it is less than minVal or
// greater than maxVal then the true block runs. The true block prints
// a message saying that the value is outside of the range and then calls
// the exit(13) function to quit the program
if ((n < minVal) || (n > maxVal)) {
cout << "Value, " << n << ", is outside of the range " << minVal << " to " << maxVal << endl;
exit(13);
}
return n;
}