Reputation: 3
I try to write a code for checking the valid of dates and days that in a text file. So before using the text file as an input, I try to write the code w/o text file as input to see whether is work or not.Here my code.
#include<iostream>
using namespace std;
bool isLeap(int year)
{
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
return (((year%4==0) && (year%100!=0)) || (year%400==0));
}
bool isValidDate(int d, int m, int y)
{
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m==4 || m==6 || m==9 || m==11)
return (d <= 30);
return true;
}
bool isLeap(int);
bool isValidDate(int,int,int);
int main()
{
int date, dd, mm, yy, years;
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
yy=years;
cout<<"Welcome, You Can Use This To Check Valid Date"<<endl;
cout<<"Insert Date"<<endl;
cin>>dd;
cout<<"Insert Month"<<endl;
cin>>mm;
cout<<"Insert Year"<<endl;
cin>>yy;
isLeap(years);
isValidDate(dd, mm, yy);
cout<<"This Is the Result:\t"<<dd<<mm<<yy<<endl;
}
I want to get the output that showing the date is valid or not but just end up showing me the date only. Is the function that I wrote not well or the way I ouput it is wrong?
By the way, is that possible to check an input dates and days on the current time?
I know is not a good code and I'm still learning. Hopefully can get some tips and advice in further.
Upvotes: 0
Views: 119
Reputation: 3155
Change this
cout<<"This Is the Result:\t"<<dd<<mm<<yy<<endl;
To this
cout<<"This Is the Result:\t"<<isValidDate(dd, mm, yy) ? "Input is Valid":"Input is Invalid"<<endl;
Upvotes: 0
Reputation: 1542
Your functions returns true or false but you haven't write a method to do what happens when those returns true/false. You've only called the functions in the main method. Use if conditions and cout for boolean results. Good luck :)
Upvotes: 0
Reputation: 401
The function is isValidDate(dd, mm, yy) is returning proper value but you are not printing its result. You are just printing whatever input you got from the user. Use this in your code..
if (isValidDate(dd, mm, yy))
{
cout<<dd<<mm<<yy<<" is valid Date"<<endl;
}
else
{
cout<<dd<<mm<<yy<<" is invalid Date"<<endl;
}
Upvotes: 1
Reputation: 33
In your main function, you could have an if condition.
if (isValidDate(dd, mm, yy)){
cout<<"Valid Date";
}
else
cout<<"Invalid Date";
Replace this instead of your last two lines in the main function.
Upvotes: 0