PDA

View Full Version : isAlpaha in C++


MarianaA
11-20-2007, 09:40 PM
Hi, I am doing a program using overloaded methods.
The program needs to find out how many days have been pass between two dates. But the user can enter the dates in two formats (11/20/2007 or November 20, 2007), so I was thinking on using isAlpha so check if the user enters the date in the second format, but I dont how to do it.
Can some one help me please.
Here is what I have.


#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

class Date
{
friend istream& operator>>(istream&, Date&);
friend ostream& operator<<(ostream&, const Date&);

private:
int mon;
int day;
int year;
string m;
public:
int difference(Date);
int operator-(Date);
bool operator>(Date);
bool isAlpha()
{
/* whatever */
return (true);
}
bool isAlnum()
{
/* whatever */
return (true);
}
};

istream& operator >>( istream& in, Date& dayz )
{
if(dayz.isAlpha()==true)
{
in>>dayz.mon;
in>>dayz.day;
in.ignore(1); //skip the ","
in>>dayz.year;
}
if(dayz.isAlnum()==true)
{
in>>dayz.mon;
in.ignore(1); //skip the "/"
in>>dayz.day;
in.ignore(1); //skip the "/"
in>>dayz.year;
}
return in;

}

Thanks :)

Antoniohawk
11-20-2007, 09:56 PM
Unless you explicitly state the format in which the date will be entered, it's gonna be hard to validate it. I'd say your best bet would be to use regular expressions. A quick Google search should turn up all kinds of results. I'm sure you could even find some regular expressions for date formats.

MarianaA
11-20-2007, 09:59 PM
The program must receive both formats