I have written a code already, and it's a little tricky. I'm trying to get it to where the user writes their date of birth (ex: 12/05/1990) and it outputs their birth as ("December 5, 1990). I have already written the else/ifs for invalid Months and invalid Days. I'm for some reason not being able to get the day and year to display. For example; it's only displaying correctly the month which is "December" and I'm getting a bunch of numbers for both Day and Year which are completely wrong. Also, any ideas of I should do for the program to be able to handle LEAP YEARS, for every 4 years. Any help is appreciated. thanks!
#include <iostream>
#include <string>
using namespace std;
struct invalidDay : public exception
{
const char * what() const throw ()
{
return "Invalid date, try again.";
}
};
struct invalidMonth : public exception
{
const char * what() const throw ()
{
return "Invalid month, try again.";
}
};
int main()
{
int day;
int month;
int year;
string monthName;
try
{
cout << "Enter birthday: mm/dd/yyyy : ";
cin >>month>>day>>year;
if (day > 31)
throw invalidDay();
if (month == 1) {
monthName = "January";
if (day > 31)
throw invalidDay();
}
else if (month == 2) {
monthName = "February";
if (day > 28)
throw invalidDay();
}
else if (month == 3) {
monthName = "March";
if (day > 31)
throw invalidDay();
}
else if (month == 4) {
monthName = "April";
if (day > 30)
throw invalidDay();
}
else if (month == 5) {
monthName = "May";
if (day > 31)
throw invalidDay();
}
else if (month == 6) {
monthName = "June";
if (day > 30)
throw invalidDay();
}
else if (month == 7) {
monthName = "July";
if (day > 31)
throw invalidDay();
}
else if (month == 8) {
monthName = "August";
if (day > 31)
throw invalidDay();
}
else if (month == 9) {
monthName = "September";
if (day > 30)
throw invalidDay();
}
else if (month == 10) {
monthName = "October";
if (day > 31)
throw invalidDay();
}
else if (month == 11) {
monthName = "Noveber";
if (day > 30)
throw invalidDay();
}
else if (month == 12) {
monthName = "December";
if (day > 31)
throw invalidDay();
}
else if (month > 12)
throw invalidMonth();
cout << monthName << " " << day << "," << year << "." << endl;
}
catch (invalidDay& e)
{
cout << e.what() << endl;
}
catch (invalidMonth& e)
{
cout << e.what() << endl;
}
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire