Help concise program please!

Hi guys,

in my program i have added validation for the month and year to be what i need. i wrote out the whole output for the if and else if for the validation. is there a way to shorten it down instead of having it copy pasted? thanks! (i'm referring to this part of the program:

if (month < 1 || month > 12)
{
cout<<endl;
cout<<"Mismatching Month/Day Values Entered"<<endl;
cout<<"Month Value Entered: "<<month<<endl;
cout<<"Day Value Entered: "<<day<<endl;
}
else if (day < 1 || day > daysInMonths[month-1])
{
cout<<endl;
cout<<"Mismatching Month/Day Values Entered"<<endl;
cout<<"Month Value Entered: "<<month<<endl;
cout<<"Day Value Entered: "<<day<<endl;
}

)

#include <iostream>

using namespace std;

int main()
{
string in;
int day,month,year,dayNum;
bool leapyear;
cout << "Enter Month: ";
cin >> month;
cout << "Enter Day: ";
cin >> day;
cout << "Enter Year: ";
cin >> year;


switch (month)
{
case 1:
dayNum=day;
break;
case 2:
dayNum=31+day;
break;
case 3:
dayNum=59+day;
break;
case 4:
dayNum=90+day;
break;
case 5:
dayNum=120+day;
break;
case 6:
dayNum=151+day;
break;
case 7:
dayNum=181+day;
break;
case 8:
dayNum=212+day;
break;
case 9:
dayNum=243+day;
break;
case 10:
dayNum=273+day;
break;
case 11:
dayNum=304+day;
break;
case 12:
dayNum=334+day;
break;
}
leapyear=false;
if(year % 4 == 0 && month>2 && !(year % 100 == 0 && year % 400 != 0))
{dayNum=dayNum+1;
}
bool result = true;
int daysInMonths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(year<2000||year>2099)
{
cout<<endl;
cout<<"Unacceptable year entered "<<year<<endl;
cout<<"Acceptable range For Year is 2000 to 2099"<<endl;
return 0;
}

if (month < 1 || month > 12)
{
cout<<endl;
cout<<"Mismatching Month/Day Values Entered"<<endl;
cout<<"Month Value Entered: "<<month<<endl;
cout<<"Day Value Entered: "<<day<<endl;
}
else if (day < 1 || day > daysInMonths[month-1])
{
cout<<endl;
cout<<"Mismatching Month/Day Values Entered"<<endl;
cout<<"Month Value Entered: "<<month<<endl;
cout<<"Day Value Entered: "<<day<<endl;
}

cout<<endl;
cout<<"Date: "<<month<<"/"<<day<<"/"<<year<<endl;
cout<<"Day Number: "<<dayNum<<endl;
}
Last edited on
Topic archived. No new replies allowed.