write a program that checks the validity of a date

Only the function to convert number of month to its name is working. Everything else gives me errors such as 0, 10, or 500 instead the day & year.

#include <iostream>
#include <string>

using namespace std;

bool isValidDay (int,int,int);
string monthToString (int);
bool isLeapYear (int);

int main(int argc, char** argv)
{
int month;
int day;
int year;

cout << "Enter month as mm " << endl;
cin >> month;
cout << "Enter day as dd " << endl;
cin >> day;
cout << "Enter year as yyyy " << endl;
cin >> year;

cout << monthToString(month)<< isValidDay(month, day, year)<< isLeapYear(year)<< endl;

return 0;
}

bool isLeapYear (int year)
{
/*A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by
100, it is not a leap year unless it is also divisible by 400.*/

if (year % 4 == 0)
{
if (year % 100 != 0)
{
return true;
}
if (year % 400 == 0)
{
return true;
}
}
return false;
}

string monthToString (int month)
{
switch(month)
{
case 1 : return "January";
break;
case 2 : return "February";
break;
case 3 : return "March";
break;
case 4 : return "April";
break;
case 5 : return "May";
break;
case 6 : return "June";
break;
case 7 : return "July";
break;
case 8 : return "August";
break;
case 9 : return "September";
break;
case 10 : return "October";
break;
case 11 : return "November";
break;
case 12 : return "December";
break;
}
}
bool isValidDay (int month, int day, int year)
{
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (day>=1 && day<=31)
// cout << month << day << year << endl;
break;

case 4: case 6: case 9: case 11:
if (day>=1 && day<=30)
//cout << month << day << year << endl;
break;

case 2:
if (day>=1 && day<=28);
// cout << month << day << year << endl;
else if (day == 29)
isLeapYear(year);
break;

default:
cout << "Invalid date." << endl;
break;
}
}
Last edited on
isLeapYear(...) should work.

isValidDay(...) doesn't because you don't have a return statement. Place the return where you have a cout. For
1
2
3
case 2:
...
return isLeapYear(year);
I changed what you said but if I enter something like 9 23 1993 it just gives me September800 or similar errors. Thanks anyways.
I updated the code

#include <iostream>
#include <string>

using namespace std;

bool isValidDay (int,int,int);
string monthToString (int);
bool isLeapYear (int);

int main(int argc, char** argv)
{
int month;
int day;
int year;

cout << "Enter month as mm " << endl;
cin >> month;
cout << "Enter day as dd " << endl;
cin >> day;
cout << "Enter year as yyyy " << endl;
cin >> year;

cout << "you entered" << monthToString(month) << isValidDay(month, day, year) << isLeapYear(year) << "which is a valid date" << endl;

return 0;
}

bool isLeapYear (int year)
{
if (year % 4 == 0)
{
if (year % 100 != 0)
{
return true;
}
if (year % 400 == 0)
{
return true;
}
}
return false;
}

string monthToString (int month)
{
switch(month)
{
case 1 : return "January";
break;
case 2 : return "February";
break;
case 3 : return "March";
break;
case 4 : return "April";
break;
case 5 : return "May";
break;
case 6 : return "June";
break;
case 7 : return "July";
break;
case 8 : return "August";
break;
case 9 : return "September";
break;
case 10 : return "October";
break;
case 11 : return "November";
break;
case 12 : return "December";
break;
}
}
bool isValidDay (int month, int day, int year)
{
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (day>=0 && day<=32)
return true;
break;

case 4: case 6: case 9: case 11:
if (day>=0 && day<=31)
return true;
break;

case 2:
if (day>=1 && day<=28);
else if (day == 29)
return isLeapYear(year);
return true;
break;

default:
cout << "Invalid date." << endl;
break;
}
return false;
}
dude, use code Brackets please.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>

using namespace std;

bool isValidDay (int,int,int);
string monthToString (int);
bool isLeapYear (int);

int main(int argc, char** argv) 
{
int month;
int day;
int year;

cout << "Enter month as mm " << endl;
cin >> month;
cout << "Enter day as dd " << endl;
cin >> day;
cout << "Enter year as yyyy " << endl;
cin >> year;

cout << "you entered" << monthToString(month) << isValidDay(month, day, year) << isLeapYear(year) << "which is a valid date" << endl;

return 0;
}

bool isLeapYear (int year)
{
if (year % 4 == 0)
{
if (year % 100 != 0)
{
return true;
}
if (year % 400 == 0)
{
return true;
}
}
return false;
}

string monthToString (int month)
{
switch(month)
{
case 1 : return "January";
break;
case 2 : return "February";
break;
case 3 : return "March";
break;
case 4 : return "April";
break;
case 5 : return "May";
break;
case 6 : return "June";
break;
case 7 : return "July";
break;
case 8 : return "August";
break;
case 9 : return "September";
break;
case 10 : return "October";
break;
case 11 : return "November";
break;
case 12 : return "December";
break;
}
}
bool isValidDay (int month, int day, int year)
{
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
if (day>=0 && day<=32)
return true;
break;

case 4: case 6: case 9: case 11:
if (day>=0 && day<=31) 
return true;
break;

case 2:
if (day>=1 && day<=28);
else if (day == 29)
return isLeapYear(year);
return true; 
break;

default:
cout << "Invalid date." << endl;
break; 
}
return false;
}
I changed what you said but if I enter something like 9 23 1993 it just gives me September800 or similar errors.
No, The output is
September10


The first 1 is the result from isValidDay(month, day, year)
The second 0 is the result from isLeapYear(year)

It is not an error. You should just improve your output (cout).
Topic archived. No new replies allowed.