Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic Need some help with this project using C++ / Branching / Switch Statement . Branching switch Statement Description Enhance the last assignment to include

Topic

Need some help with this project using C++ / Branching / Switch Statement .

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 :

#include

using namespace std;

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

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

bool leapyear = false;

if ((year % 4) == 0)

{

leapyear = true;

}

if (year<2000 || year> 2099) { cout <<"year is valid " < } 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 "< } else { cout << "Invalid Month Entered "< }

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

} // Validating Other Day Values

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

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

}

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

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

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

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

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

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

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

} 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 project using C++ / Branching / Switch Statement .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 10th International Symposium Sstd 2007 Boston Ma Usa July 2007 Proceedings Lncs 4605

Authors: Dimitris Papadias ,Donghui Zhang ,George Kollios

2007th Edition

3540735399, 978-3540735397

More Books

Students also viewed these Databases questions