2
\$\begingroup\$

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);
        }

    }

    }




    }
\$\endgroup\$
6
\$\begingroup\$

The corrected function does two things:

  • Sanitizes the day, month, year parameters
  • Calls print24

It would be better to follow the single responsibility principle, make it do just one thing. It will make sense to rename it to sanitize_day_month_year, for example, and move the print24 outside.


When you have code like this:

if (some_boolean_expression) {
    return true;
}
else {
    return false;
}

Simply return the boolean expression itself:

return some_boolean_expression;
\$\endgroup\$
5
\$\begingroup\$

There are many ways to improve this code other than making it shorter.

First, it is a good idea to not include using namespace std;. To call a function in a namespace without using it, put the name of the namespace followed by :: in front of the function name, like std::cout << value;

Second, use good spacing practices:

void corrected (int &d , int &m , int  &y)
{

 if(d<1)
    {


    d=1 ;

You have extra, unnecessary line breaks, erratic spacing between operators, and even a space before your semicolon. You should use a single space between all operators for ease of reading, one extra line at most between sections, and standard indentation. This is a better version of the above:

void corrected (int &d, int &m, int &y)
{
    if(d<1)
    {
        d = 1;

Third, you are duplicating code left and right in the corrected() function. Why are you nesting your examinations of the day, month, and year variables? Just check them separately, like this:

void corrected (int &d, int &m, int &y)
{
    if(d < 1) {
        d = 1;
    }
    else if(d > 30) {
        d = 30;
    }

    if(m < 1) {
        m = 1;
    }
    else if(m > 12) {
        m = 12;
    }

    if(y < 2000) {
        y = 2000;
    }
    else if(y > 2015) {
        y = 2015;
    }

    print24(d, m, y);
}

Notice how I only check each variable once, I don't nest all my checks and make myself check several times based on how the program runs.

Also, it is a good idea to give your variables more descriptive names. It is clear what d, m, and y stand for, but it would be just as easy to write out day, month, and year. Also, corrected() should specify that you are correcting a date, like correct_date().

Also, in a real program, never ever modify a bad input like this - you will confuse your users, and probably chose a wrong value anyway; instead, make the user input the value again.

In valid(), you are just begging for bugs. You should use correct spacing around your operators, braces around the if/else blocks, and indentation; additionally, it would be a good idea to do your checks in the same order, large/small or small/large, not a combination. This is a cleaned-up version:

bool valid (int &d, int &m, int &y)
{
    if (d >= 1 && d <= 30 && m >= 1 && m <= 12 && y >= 2000 && y <= 2015) {
        return true;
    }
    else {
        return false;
    }
}

You should be able to use these tips to rework the rest of your code.

\$\endgroup\$
  • \$\begingroup\$ hey man thank u so so much god bless u and protect u \$\endgroup\$ – fahad Mar 13 '15 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.