Latina <sd****@gmail.comwrote:
Hi, I am doing a program that deals with dates, so the program needs
to check if the user enters the date with numbers or letters.
I try isAlpha() and isAlnum() but it is given me an error message.
Can some one help me please.
Error:
'class Date' has no member named 'isAlpha'
'class Date' has no member named 'isAlnum'
Here is my code:
class Date
{
friend istream &operator>>(istream&, Date &);
friend ostream &operator<<(ostream&, const Date &);
private:
int mon,day,year;
public:
Date(){mon=day=year=0;}
int operator-(const Date&)const;
void print();
};
istream& operator>>(istream &in, Date &d)
{
This function is supposed to load data from 'in' into 'd'. At this point
in the code the data in 'd' is meaningless. Asking if d.isAlpha() is
useless.
What you want to know is if the data contained in the 'in' variable is
letters or numbers...
if(d.isAlpha())
{
in>>d.mon;
in>>d.day;
in.ignore(1); //skip the ","
in>>d.year;
}
if(d.isAlnum())
{
in>>d.mon;
in.ignore(1); //skip the "/"
in>>d.day;
in.ignore(1); //skip the "/"
in>>d.year;
}
return in;
}
I suggest you start smaller. Something like this:
// put includes here
class Date {
int month, day, year;
public:
Date() { month = day = year = 0; }
int getMonth() const {
return month;
}
int getDay() const {
return day;
}
int getYear() const {
return year;
}
istream& readAsNum( istream& );
};
istream& Date::readAsNum( istream& in )
{
// place code here
return in;
}
int main() {
stringstream ss( "11/17/2007" );
Date result;
result.readAsNum( ss );
assert( result.getMonth() == 11 );
assert( result.getDay() == 17 );
assert( result.getYear() == 2007 );
cout << "OK!\n";
}
Take the above code and paste it into your cpp file. Then add code where
indicated until it prints "OK!" on the screen. Once you have done that,
come back here and post what you have so far for more help.