-
Range
Hi,
Can any one please let me know where I am going wrong??I am trying this for past 2 hrs ,but .......
the problem is accept the date of Birth seprately dd,mm yyyy
but the age should be btw 15 to 70 yrs on 31/12/2001. if its outside the range error message should be displayed.....Again there it should check whether its a leap yr or not......
Though i havent given a thought about a leap yr ......pls give me hints what should I do??
Thanks
Code:
cout << "Input Year : ";
aYear = readLine();
year= atoi(aYear.c_str());
if(year>1900 || year <2002)
{
validAge(year); //function
cout<<"Age not within range"<<endl;
cout<<"data not added not database"<<endl;
}
void validAge(const int aYear)
{
const int mini= 1986;
const int max= 1931;
if (aYear>mini)
{
cout<<" too young"<<endl;
}
else if(aYear<max )
{
cout<<"too young"<<endl;
}
}
-
Quote:
aYear = readLine();
...don't you mean: aYear.readLine(); ?
-
-
Help
Hi
readLine is a function,that we have been told to use......
Code:
string readLine()
{
char buffer[SIZE];
cin.getline(buffer, SIZE, '\n');
return buffer;
}
that funcn is not creating any problem,I am sure I am missing soemthing on the logic.......Pls help......
I just dont know how tell validate the date btw 15 yrs and 70 yrs..
also to check about the leap year ......
thanks
-
The problem is, you create a variable, buff, within the function, and then try to return it. This is not good. The variable will be destroyed at any time after leaving the function! The solution is to allocate with "new". Also, you show the function to be returning a string, but instead it returns a char[]. That won't work.
Try:
Code:
string readLine()
{
string value = new string; //...allocate memory...
char buffer[SIZE];
cin.getline(buffer, SIZE, '\n');
value = buffer; //...operator overload copies contents to "value"...
return value;
}
-
Also, validAge is directed to print "too young" regardless of the condition! And you should return a value for this function...
Code:
...
int valid;
if(year>1900 || year <2002)
{
valid = validAge(year); //function
if(valid == 0)
{
cout<<"Age not within range"<<endl;
cout<<"data not added not database"<<endl;
}
}
...
int validAge(const int aYear)
{
const int mini= 1986;
const int max= 1931;
if (aYear>mini)
{
cout<<" too young"<<endl;
return 0;
}
else
if(aYear<max )
{
cout<<"too old"<<endl; //cout<<"too young"<<endl;
return 0;
}
return 1;
}
-
Thanks
Thanks a lot for pointing out the flaws in the code....
Pls. can u give me some idea about ,how to check the leap year....
in the funcn........Thanks once again.
-
Leap Year
Because of the range of years that you're using, if the year is evenly divisible by four, it's a leap year. This would not be sufficient criteria for a much greater range of years but will suffice in this case.
:)