Write a C++ program that will prompt the user to enter a date and determine whet
ID: 3624076 • Letter: W
Question
Write a C++ program that will prompt the user to enter a date and determine whether the date is a valid one. If the program is given a valid date, an appropriate message should be displayed. If the user enters an invalid date, a message explaining how the date is invalid should be displayed.Five different sample runs are as follows:
Please enter a date (mm dd yyyy): 4 30 2000
4/30/2000 is a valid date.
Please enter a date (mm dd yyyy): 13 1 1998
Invalid month: 13
Please enter a date (mm dd yyyy): 2 29 1997
Invalid day of month: 29
Please enter a date (mm dd yyyy): 4 33 1995
Invalid day of month: 33
Please enter a date (mm dd yyyy): 13 17 2015
Invalid month: 13
Invalid year: 2015
For this program, you should assume that valid years are those between 1901 and 2010, inclusive. For this range, leap years are those years that are evenly divisible by 4. Valid months are between 1 and 12, inclusive.
Valid days depend upon which month. Remember that January, March, May, July, August, October, and December all have 31 days. April, June, September, and November have 30 days. February either has 28 or 29 days, depending upon whether the year is a leap year or not.
Program Requirements:
1) switch statement to validate day numbers.
2) At least two boolean variables.
3) Comments to include your name, class, assignment number, point value, etc. Also, use comments to explain the action of your code.
Note: If the month number is invalid, you do not need to validate the day number. For example, if the month number is 13, we cannot check for correct day numbers. However, valid month numbers do correlate to a certain range of valid day numbers.
Explanation / Answer
please rate - thanks
import java.util.*;
public class validdate
{
public static void main(String [] args)
{int month, day, year,days;
boolean leapy=false,gooddate=true;
Scanner in=new Scanner(System.in);
System.out.print("Please enter a date (mm dd yyyy): ");
month=in.nextInt();
day=in.nextInt();
year=in.nextInt();
if(month<1||month>12)
{System.out.println("invalid month "+month);
gooddate=false;
}
else
{days=getDaysInMonth(month,year);
if(day<1||day>days)
if(month!=2)
{System.out.println(" Invalid day of month "+day);
gooddate=false;
}
else
{if(day>29)
{System.out.println(" Invalid day of month "+day);
gooddate=false;
}
leapy=leap(year);
if(!leapy&&day>28)
{System.out.println(" Invalid day of month "+day);
gooddate=false;
}
}
}
if(year<1901||year>2010)
{ System.out.println("Invalid year "+ year);
gooddate=false;
}
if(gooddate)
System.out.println(month+"/"+day+"/"+year+" is a valid date");
}
public static boolean leap(int year)
{if(year%400==0)
return true;
if(year%100==0)
return false;
if(year%4==0)
return true;
return false;
}
public static int getDaysInMonth(int month,int year)
{int days;
switch(month)
{case 9:
case 4:
case 6:
case 11: days=30;
break;
case 2: if(leap(year))
days=29;
else days=28;
break;
default: days=31;
}
return days;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.