Using C++: Code Program 2 only Wtite a program that prompts the user to enter a
ID: 3808064 • Letter: U
Question
Using C++: Code Program 2 only
Explanation / Answer
Code:-(Program 2)
#include <iostream>
#include <string>
#include <exception>
using namespace std;
int day;
int month;
int year;
class invalidDay : public exception{
public:
const char * what() const throw()
{
return "The value of day that you entered is invalid, try again.";
}
};
class invalidMonth : public exception{
public:
const char * what() const throw()
{
return "The value of month that you entered is invalid, try again.";
}
};
class invalidYear : public exception{
public:
const char * what() const throw()
{
return "The value of year that you entered is invalid, try again.";
}
};
int main()
{
int check=1;
char month_words[10];
while(check){
cout << " Enter your birthday here in this format: mm/dd/yyyy--->: ";
cout <<"Enter day here.";
cin >> day;
cout <<"Enter the month here.";
cin >> month;
cout <<"Enter the year here.";
cin >> year;
try{
try{
try{
if (day > 31){
invalidDay ed;
throw ed;
}
}
catch(invalidDay& x)
{
cout <<x.what()<<endl;
continue;
}
if(month==1)
cout<<"january";
else
if(month==2)
cout << "February ";
else
if(month==3)
cout << "March ";
else
if(month==4)
cout << "April ";
else
if(month==5)
cout << "May ";
else
if(month==6)
cout << "June ";
else
if(month==7)
cout << "July ";
else
if(month==8)
cout << "August ";
else
if(month==9)
cout << "September ";
else
if(month==10)
cout << "October ";
else
if(month==11)
cout << "November ";
else
if(month==12)
cout << "December ";
else
if (month > 12){
invalidMonth em;
throw em;
}
}
catch (invalidMonth& m)
{
cout <<m.what()<<endl;
continue;
}
if(year<1915 || year>2017){
invalidYear ey;
throw ey;
}
}
catch(invalidYear& y){
cout<<y.what()<<endl;
continue;
}
check=0;
}
cout << day << "," << year << "." << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.