C++ / Branching/ Switch statement


#1

Topic

Branching

switch Statement

Description

Enhance the last assignment to include input validation. The enhanced assignment should check the values of day, month, and year entered by the user to ensure that they are OK. If they are not OK, the program should display an error message and return. The program should check the input values before computing the day number.

The program should validate the input values to ensure that:

· Year value is from 2000 to 2099 inclusive.

· Month value is from 1 to 12 inclusive.

· Day value is valid for the given month and the year.

Testing

Input Test Run 1

Enter Month: 4

Enter Day: 31

Enter Year: 2003

Output Test Run 1

Mismatching Month/Day Values Entered.

Month Value Entered: 4

Day Value Entered: 31

Input Test Run 2

Enter Month: 3

Enter Day: 31

Enter Year: 201

Output Test Run 2

Unacceptable Year Entered 201.

Acceptable range For Year is 2000 to 2099

Input Test Run 3

Enter Month: 2

Enter Day: 29

Enter Year: 2003

Output Test Run 3

Mismatching Month/Day Values Entered.

Month Value Entered: 2

Day Value Entered: 29

Input Test Run 4

Enter Month: 2

Enter Day: 29

Enter Year: 2004

Output Test Run 4

Date: 2/29/2004

Day Number: 60

Input Test Run 5

Enter Month: 3

Enter Day: 1

Enter Year: 2003

Output Test Run 5

Date: 3/1/2003

Day Number: 60

Input Test Run 6

Enter Month: 3

Enter Day: 1

Enter Year: 2004

Output Test Run 6

Date: 3/1/2004

Day Number: 61

Sample Code

/*

Java Code

*/

int day, month, year;

 

//input day, month, and year values here

 

 

 

 
//Validating Year Value

if ( year < 2000 || year > 2099 )

{

          JOptionPane.showMessageDialog

               (null, “Unacceptable Year Entered: “ + year );

JOptionPane.showMessageDialog

               (null, “Acceptable range For Year is 2000 to 2099 “ );

 

          System.return (0);

}

 

//Determining Leap year

boolean leapyear = false;

 if  ((year % 4) == 0)

{

          leapyear = true;

}

 
//Validating Month Value

if ( month < 1 || month > 12 )

{

          JOptionPane.showMessageDialog

                                (null, “Invalid Month Entered: “ + month );

          System.return (0);

}

 
//Validating YearValue

if ( year < 2000 || year > 2099 )

{

          JOptionPane.showMessageDialog

                               (null, “Unacceptable Year Entered: “ + year );

          JOptionPane.showMessageDialog

                              (null, “Valid Range For Year 2000 to 2099 );

System.return (0);

}

 
//Validating Day Value

if ( ( day < 1 ) || ( day > 31 ) )

{

          JOptionPane.showMessageDialog

                                (null, “Invalid Day Entered: “ + day );

          System.return (0);

}

 
//Validating Other Day Values

if (  ( month == 4 || month == 6 || month == 9 || month == 11)  && ( day > 30 ) )

{

          JOptionPane.showMessageDialog

                               (null, “Mismatching Month/Day Values Entered. “  );

          JOptionPane.showMessageDialog

                               (null, “Month Value Entered: “ + month );

JOptionPane.showMessageDialog

                      (null, “Day Value Entered: “ + day );

          System.return (0);

}

 

if ( (month == 2) && ( day > 29 ) && ( leapyear ) )

{

          JOptionPane.showMessageDialog

                              (null, “Mismatching Month/Day Values Entered: “  );

          JOptionPane.showMessageDialog

                             (null, “Month Value Entered: “ + month );

JOptionPane.showMessageDialog

                 (null, “Day Value Entered: “ + day );

          System.return (0);

}

 

if ( (month == 2) && ( day > 28 ) && ( ! leapyear ) )

{

          JOptionPane.showMessageDialog

                               (null, “Mismatching Month/Day Values Entered: “  );

          JOptionPane.showMessageDialog

                              (null, “Month Value Entered: “ + month );

JOptionPane.showMessageDialog

                    (null, “Day Value Entered: “ + day );

          System.return (0);

}
/*

C++ Code

*/

 

/*

The code below asks the user to input month, day and year one after the other. It then validates the values entered. If any of the values is invalid, it displays an error message and returns.

*/

 

int day, month, year;

 

//input day, month, and year values here.

 

 

 

 

//Validate input values

//Determining Leap year

Bool leapyear = false;

 if  ((year % 4) == 0)

{

          leapyear = true;

}

 
//Validating YearValue

if ( year < 2000 || year > 2099 )

{

cout <<  “Unacceptable Year Entered: “ << year << endl;

          cout << “Valid Range For Year 2000 to 2099 << endl;

return (0);

}

 
//Validating Month Value

if ( month < 1 || month > 12 )

{

         

           cout << “Invalid Month Entered: “ << month << endl;

           return (0);

}

 
//Validating Day Value

if ( ( day < 1 ) || ( day > 31 ) )

{         

cout << “Invalid Day Entered: “ <<  day << endl;

          return (0);

}

 
//Validating Other Day Values

if (  ( month == 4 || month == 6 || month == 9 || month == 11)  && ( day > 30 ) )

{

          cout << “Mismatching Month/Day Values Entered. “  << endl;

          cout << “Month Value Entered: “ << month << endl;

cout <<  “Day Value Entered: “ << day << endl;

          return (0);

}

 

if ( (month == 2) && ( day > 29 ) && ( leapyear ) )

{

          cout << “Mismatching Month/Day Values Entered: “  << endl;

          cout << “Month Value Entered: “ << month << endl;

cout <<  “Day Value Entered: “ << day << endl;

          return (0);

}

 

if ( (month == 2) && ( day > 28 ) && ( ! leapyear ) )

{

          cout << “Mismatching Month/Day Values Entered: “ << endl;

          cout << “Month Value Entered: “ << month << endl;

cout <<  “Day Value Entered: “ << day << endl;

          return (0);

}

My code :slight_smile:

#include <iostream>

using namespace std;

int main()
{
    int day, month, year;

     cout<<"Enter month"<<endl;
     cin>> month;
     cout<<"Enter day" << endl;
     cin>>day;
     cout <<"Enter year"<<endl;
     cin>> year;

     bool leapyear = false;

 if  ((year % 4) == 0)

{

          leapyear = true;

}

 if (year<2000 || year > 2099)
 {
     cout <<"year is valid " <<endl;
 }
 else
 {


     cout <<"Unacceptable Year Entered "<< year << endl;

     cout <<"Valid Range For Year 2000 to 2099"<< endl;
 }

 if (month<1 || month> 12)
 {
     cout <<"month is valid "<<endl;
 }
 else
 {
     cout << "Invalid Month Entered "<<month<<endl;
 }

 // Validating Day Value
 if ((day<1) || day>31)
 {
     cout <<"day is valid "<<endl;
 } else
 {
     cout <<" Invalid Day Entered "<<day<<endl;

 }
 // Validating Other Day Values

 if ((month == 4 || month == 6 || month == 9 || month == 11) && (day >30))

 {
     cout << "month/Day is valid "<<endl;
 }
 else
     cout << "Mismatching Month/Day Values Entered:"<<endl;
     cout <<" Month Value Entered:" << month << endl;
     cout <<"Day Value Entered:"<< day << endl;


 }


if ( (month == 2) && ( day > 29 ) && ( leapyear ) )

{
    cout <<"Month /day values are valid "<<endl;
}else


          cout << “Mismatching month/day Values Entered: “  << endl;

          cout << “Month Value Entered: “ << month << endl;

cout <<  “Day Value Entered: “ << endl;

          return (0);

}



if ( (month == 2) && ( day > 28 ) && ( ! leapyear ) )

{
    cout << "month/day values are valid" <<endl;

}
else

          cout << “Mismatching Month/Day Values Entered: “ << endl;

          cout << “Month Value Entered: “ << month << endl;

cout <<  “Day Value Entered: “ << day << endl;

    return 0;
}

Need some help with this Assignment before the deadline.


#2

Posting the assignment in hopes that someone will do (part of) your work is almost certainly not something you’re allowed to do in your assignment. It’s also not something people are likely to want to help with, because it appears that you’re more interested in your grade than in learning

Ask questions about what information you’re missing instead, so that you can do the assignment yourself

You can certainly ask for help, but you need to be specific about what you want to know, and word your questions so that you’re not asking for something to be done for you, but in a way that gets you information which allows you to do it yourself

A piece of general advice: If error messages are overwhelming, or you’re otherwise not able to figure out which part is “wrong”, then work on a smaller piece of code at a time, move the problematic part into its own file so that there is less going on and less to look at.