I cannot get the correct inputs.

I am currently working on a project that requires me to make a program that validates a date, with the leap year included.

I have this so far

#include <iostream>

using namespace std;

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

cout<< "Enter the numeric value for the month, day and year of the date"; cin >> month >> day >> year;
if(month <= 1, month >= 12)
{
cout << "The year is valid" << endl;
}
else
{
cout << "The year is not valid" << endl;
}


return 0;
}
Right now I am just trying to start off with the month, but when I put in the cin for the month, day, and year I do not get the correct output. It just multiplies the inputs together. Where am I going wrong so far?

Thanks for the help!!
Ok, some of this syntax doesn't make sense, let's make this program a little clearer:

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
#include <iostream>

using namespace std;

int main()
{
int month, day, year; // I recommend always initializing your variables but whatever

cout<< "Enter the numeric value for the month: ";
cin >> month;

cout << " Day: ";
cin >> day;

cout << " Year: ";
cin >> year;

// check for date validity
if ( month > 12 || month < 1 ) // if the month is greater than 12 OR less than 1, it's invalid
{
cout << "The month is not valid" << endl;
}
else
{
cout << "The month is valid" << endl;
}

return 0;
}


Use similar if statements to check the bounds of year and day (only month is tested in the above code) or you can include all the conditions in the same if statement using the or-operator ( || ) and the and-operator ( && )

I would recommend checking all the date bounds in one if-statement and cout whether or not the entire date is valid since there aren't many things that can cause the date to be invalid. If you were checking inputs of many variables then it might be easier to check values individually for finding out what specific variables are failing.
Last edited on
Topic archived. No new replies allowed.