Hello, I'm currently learning how to program using c++ and this is one of the exercises my teacher gave to me. I already made a code for it but for some reason when I enter a valid date...the output will still result to "invalid date" even tho it's valid. Please check the code I've made if there any errors.
In this lab, you add the input and output statements to a partially completed C++ program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
bool validateDate(int, int, int);
using namespace std;
int main()
{
int year;
int month;
int day;
const int MIN_YEAR = 0, MIN_MONTH = 1, MAX_MONTH = 12, MIN_DAY = 1, MAX_DAY = 31;
bool validDate = true;
cout << "Enter Month: ";
cin >> day;
cout << "Enter Day: ";
cin >> month;
cout << "Enter Year: ";
cin >> year;
if(year <= MIN_YEAR) // invalid year
validDate = false;
else if (month < MIN_MONTH || month > MAX_MONTH)
validDate = false;
else if (day < MIN_DAY || day > MAX_DAY) // invalid day
validDate = false;
if(validDate == true)
{
cout << day << "/" << month << "/" << year << " is a valid date.";
}
else
{
cout << day << "/" << month << "/" << year << " is an invalid date.";
return 0;
}
|
Format of the output:
month/day/year is a valid date.
month/day/year is an invalid date.
The testing values:
month = 9, day = 21, year = 2002 ===> keeps saying it's invalid
month = 5, day = 32, year = 2014
Please help me :D