67 095
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #50782 in C++ for zexy

Question #50782
Write a main()function that instantiates at least five Date objects and accepts data for
each. When you run the program, provide data that tests that each type of exception is
being recognized. The main()function should catch the exceptions and display an appro-
priate error message about each including what the entered value was. If an exception is
caught, the data for the Date object should be set to the default date 1/1/2010. Save the file
as DateExceptions.cpp.
Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib> 
#include <string>
using namespace std;
//array that stores the number of days for each month of non-leap year
const int DAYSINMONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
class DateException : public runtime_error{
private:
int year;
int month;
int day;
public:
explicit DateException (int _year, int _month, int _day, const string& msg="") : runtime_error(msg){
year = _year;
month = _month;
day = _day;
}; 
int GetYear() {return year;}
int GetMonth() {return month;}
int GetDay() {return day;}
};
class Date{
private:
int year;
int month;
int day;
public:
Date() {year = 0; month = 0; day = 0;}
void Input() {
bool leapOrNot = false;

// Year must be greater than 0 (To check for leap)
cout<<"Enter year : ";
cin>>year;
if (year < 1) throw DateException(year , month ,day ,"Error!!Year must be greater than 0!!!");
// check for leap
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) leapOrNot = true;
else
leapOrNot = false;
cout<<"Enter month : ";
cin>>month;
if (month < 1 || month > 12 ) throw DateException(year , month ,day ,"Error!!Month is out of range!!!");

cout<<"Enter day : ";
cin>>day;
// if the year is a leap year and the month of February
if (month == 2 && leapOrNot) {
if (day < 0 || day > 29) throw DateException(year , month ,day ,"Error!!Day is out of range!!!");
}
else
{
if (day < 0 || day > DAYSINMONTH[month - 1]) throw DateException(year , month ,day ,"Error!!Day is out of range!!!");
}
}
void SetDateToDefault(void){
year = 2010;
month = 1;
day = 1;
}

friend ostream& operator<<(ostream& stream, const Date& obj){

stream <<"----------------------------------\n"<<endl;
stream <<obj.day<<"/"<<obj.month<<"/"<<obj.year<<endl<<endl;
   stream <<"----------------------------------\n"<<endl;

return stream;
}
};
 
int main () { 
Date date[5];
for (int i = 0 ;i < 5; i++)
{
try{
date[i].Input();
}
catch(exception& exp)
{
cout<<"Was caught exception!!"<<endl;
cout<<exp.what()<<endl;
cout<<"Date will be set by default"<<endl;
date[i].SetDateToDefault();
}
}
cout<<endl<<"Dates : "<<endl;
for (int i = 0; i < 5; i++)
{
cout<<date[i];
}
system&#40;"pause"&#41;;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions