Re: Validating Social Security integers. Help!!!!!!!!!
On Apr 2, 6:07 pm, rlueneb...@gmail.com wrote:
Finally works after your suggestions. I just changed a couple of
things. The code looks cleaner too and I don't have to deal with
integer inputs. I thought that storing SSN as integer data member
would be easier to manipulate.
It is, in a way. If you store it as an int, it's a lot easier
to multiply two SSN's, for example. "Easier to manipulate"
isn't necessarily an advantage, until you define the
manipulations.
But turns out to be a problem. I don't
like to save formated data in data members but that is part of the
assignment. Please send your comments.
#include <iostream>
#include <string>
#include "stdafx.h"
#include <ctype.h>
using namespace std;
class Employee
{
protected:
Just a question: why protected? I rarely have protected data,
and then there is a very good reason. In this case, you don't
have a virtual destructor, so it is probable that the class
isn't meant to be inherited from anyway. As a general rule,
everything should be as restricted as possible.
// Full Name
string name;
// Employee Number
char EmployeeNumber[5];
Which is a wierd declaration. What is it, text or a number?
// Social Security Number
string SocialSecurity;
// Hire Date
string HireDate;
public:
// constructor
Employee()
{
SocialSecurity="";
}
string getSSN(string MySSN)
{
return FormatSSN(MySSN);
}
string getName(string MyName)
{
name="";
return MyName;
}
// Destructor
~Employee() {}
bool IsValidSSN(string SSN)
{
bool valid = true;
if (SSN.length()> 9)
{
valid=false;
}
That has to be wrong. At the very least, you should allow
leading and trailing spaces.
char c[10];
strncpy (c,SSN.c_str(),10);
for (int i=0; i<9; i++)
{
if (!isdigit(c[i]))
{
valid=false;
break;
}
}
return valid;
}
string FormatSSN(string SSN)
{
char c[10];
strncpy (c,SSN.c_str(),10);
for (int i=0; i<9; i++)
{
// Converts char to valid string
std::string s(1, c[i]);
if (i==2)
{
SocialSecurity = SocialSecurity + s + "-";
}else if (i==4) {
SocialSecurity = SocialSecurity + s + "-";
}else{
SocialSecurity = SocialSecurity + s;
}
}
return SocialSecurity;
}
What you've written is simply the equivalent of:
SocialSecurity = SSN.substr( 0, 3 )
+ '-' + SSN.substr( 3, 2 )
+ '-' + SSN.substr( 5 ) ;
Why not write it that way, and be done with it?
Also: what is the correct format: a string of digits, or a
string of digits with some separators? You don't want to output
one format, and require input in another. (The obvious way of
validating the input would be to use boost::regex. I'd reformat
it to the target format at the same time, catching the numeric
substrings in the regular expression, and pasting them together
more or less like the above.)
};
class EmployeePay: public Employee
EmployeePay is an Employee? Either the names are wrong, or using
inheritance is wrong.
And of course, if you're going to do this sort of derivation,
you need a virtual destructor in the base class.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34