Question:
Write a function that accepts integer values for day, month and year and returns them in a valid format. If the day is above 30, then convert it to 30. If the month is above 12 then convert it to 12. Similarly, if day or month is below 1, then it converts them to 1. Finally it ensures that the years are between 2000 and 2015. If all inputs are valid then it returns true, else it returns false. Write a program that prompts the user to input a day, month and year. The program will validate them using the function written above and displays valid if all the inputs are valid. Otherwise, the program displays invalid and the corrected values of input returned by the function. Use pass by reference method.
If the input day is < 1 the day should be corrected to day = 1.
If d > 30 the day will be day = 30 and if input the user month should be between 1 and 12 also the year (between 2000 and 2015).
I need to make my code smaller, especially the function of corrected.
I made three functions: one to print the input, a second for corrected data, and a third to validate the input of the user.
#include<iostream>
using namespace std ;
void print24(int &d , int &m, int &y);
bool valid (int &d , int &m , int & y);
void corrected (int & d , int & m , int & y);
int main()
{
int day , month , year ;
cout<<"Enter Date between 1/1//2000 to 30/12//2014 in format <D M Y>" << endl ;
cin>>day>>month>>year ;
cout<<"Date Entered : ";
print24(day ,month , year);
cout<<endl ;
if(valid( day , month , year))
{
cout<<"You Entered Valid Date :";
print24(day ,month , year);
}
else
{
cout<<endl<<"You Entered inValid Date ! "<<endl;
cout<<"Corrected Date is : ";
corrected (day , month , year);
}
}
void print24(int &d , int &m , int & y)
{
cout<<d<<"/"<<m<<"/"<<y;
}
bool valid (int &d , int &m , int & y)
{
if (d<=30 && d >=1 && m <=12 && m>=1 && y>=2000 && y<=2015)
return true ;
else
return false ;
}
void corrected (int &d , int &m , int &y)
{
if(d<1)
{
d=1 ;
if(m<1)
{
m=1;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else if(m>12)
{
m=12 ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else
{
m=m ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
else if(d>30)
{
d=30 ;
if(m<1)
{
m=1;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else if(m>12)
{
m=12 ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else
{
m=m ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
}
///////////////////////////////////////////////////////////////////////////////////
else
{
d=d ;
if(m<1)
{
m=1;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else if(m>12)
{
m=12 ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
else
{
m=m ;
if(y<2000)
{
y=2000;
print24(d ,m , y);
}
else if(y>2015)
{
y=2015 ;
print24(d ,m , y);
}
else
{
y=y ;
print24(d ,m , y);
}
}
}
}