Nov 5, 2015 at 5:43am UTC
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 Nov 5, 2015 at 5:44am UTC
Nov 5, 2015 at 5:06pm UTC
I changed what you said but if I enter something like 9 23 1993 it just gives me September800 or similar errors. Thanks anyways.
Nov 5, 2015 at 5:24pm UTC
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;
}