Question

In: Computer Science

in C++: In date calculations an important consideration is whether or not the year is a...

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;
}

Solutions

Expert Solution

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;

}


Related Solutions

Question 1: Write a C program that reads a date from the keyboard and tests whether...
Question 1: Write a C program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid month...
Why is ethics an important consideration in marketing decisions?
Why is ethics an important consideration in marketing decisions?
Assume that you are deciding whether to acquire a four-year university degree. Your only consideration at...
Assume that you are deciding whether to acquire a four-year university degree. Your only consideration at this moment is the degree as an investment for yourself. Costs per year are tuition fees of $600 and books at $100. The government also pays to the university an equivalent amount to your tuition fees to cover the real cost. If you don't go to university, you could earn $6000 per year as an acrobat. With a university degree, however, you know that...
Why is it important to take into consideration the genre, audience and purpose of the final...
Why is it important to take into consideration the genre, audience and purpose of the final document? Do you believe you can do proper research without knowing the genre, audience or purpose? Why or why not?
Is government stability an important consideration when giving foreign aid?
Critical Thinking 1. What kind of foreign aid is effective? Why? 2. Is government stability an important consideration when giving foreign aid? 3. What role does corruption play in the effectiveness of foreign aid?
Why is molecular bonding an important consideration inn the manufacture of plastics?
Why is molecular bonding an important consideration inn the manufacture of plastics?
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
A system description that reveals whether a system is stable or not stable without consideration of...
A system description that reveals whether a system is stable or not stable without consideration of other system attributes. A. Stable system B. Absolute Stability C. Marginally stable D. Optimum control system
Your text authors note that it is important to place consideration of the specific meaning of...
Your text authors note that it is important to place consideration of the specific meaning of a good life in the context of a. personality and genetic differences b. differences in values and religious orientation. c. different cultures and stages of lifespan development. d. life events and availability of resources and opportunities. According to your textbook authors, what is “new” and unique about positive psychology is that it has a. helped clarify the relative independence of the “good” and the...
most important part c) Determining whether a quantified logical statement is true and translating into English,...
most important part c) Determining whether a quantified logical statement is true and translating into English, part 2. infoAbout In the following question, the domain of discourse is a set of male patients in a clinical study. Define the following predicates: P(x): x was given the placebo D(x): x was given the medication A(x): x had fainting spells M(x): x had migraines Suppose that there are five patients who participated in the study. The table below shows the names of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT