The process to create your own is not hard. Have the user enter in some text and get it using cin.getline() and store the result in a buffer. Then run though the input looking for digits where there should be digits, and seperators where there should be seperators.
Here is an example...
CODE
#include <iostream>
using namespace std;
//This function is just used to determine if a character is a ASCII digit.
inline int isDigit(char cIn) {return (cIn >= '0' && cIn <='9'); }
//This function determins the seperators used in the SSN.
// you can add to this function to allow different input formats.
inline int isSeperator(char cIn) {return (cIn=='-' ); }
//This function will parse the input for a valid SSN,
// if the string SSN does not hold a valid SSN then
// this function return a 0 (false) else it returns a
// long int containing the SSN.
long validateSSN(char *SSN);
//This union is used to extract a SSN encoded into a long.
typedef union
{
long hold;
struct
{
unsigned group:8;
unsigned area:10;
unsigned serial:14;
};
} SSNumber;
int main()
{
SSNumber SSN;
char buffer[256];
do
{
cout <<"Input your SSN (###-##-####): ";
cin.getline(buffer, 255);
SSN.hold = validateSSN(buffer);
if (!SSN.hold)
{
cout << "\nInvalid SSN!!!\n";
}
} while (!SSN.hold);
cout << "You entered: ";
cout << SSN.area << "-" << SSN.group << "-" << SSN.serial <<"." << endl;
return 0;
}
long validateSSN(char *SSN)
{
SSNumber RetSSN;
long retVal = true;
int SSN_Numbers[] = {0, 0, 0}; //used to hold the numbers...
int SSN_Digits[] = {3,2,4}; //keep track of how many digits we have read.
int i, j; //counters, i loops though the buffer, and j counts how many sections in the SSN.
i=0; j=0;
do
{
if (isDigit(SSN[i]))
{
do
{
SSN_Numbers[j] *= 10;
SSN_Numbers[j] += SSN[i]-'0';
i++;
} while (--SSN_Digits[j] && isDigit(SSN[i]));
//this line allows the user to enter: 123456789 w/o the '-'
// if it is removed then the user MUST enter the '-' char between segments.
if ((SSN_Digits[j]==0) && isDigit(SSN[i])) { j++; }
} else if (isSeperator(SSN[i]))
{
if (j<2)
{
j++;
i++;
} else
{
//ERROR we have too many sections!!!
retVal = 0;
}
} else
{
//ERROR unexpected character
retVal = 0;
}
} while (j<3 && SSN[i] && retVal);
//we want to ensure that there were 3 sets of digits (thus j==2)
// and that retVal has not been set to zero (false).
if(j==2 && retVal)
{
cout << SSN_Numbers[0] << "." << SSN_Numbers[1] << "." <<SSN_Numbers[2]<<"\n";
RetSSN.area=SSN_Numbers[0]; //Assign the first 3 to the area code...
RetSSN.group=SSN_Numbers[1]; //Assign the next 2 to the group code...
RetSSN.serial=SSN_Numbers[2]; //Assign the last 4 to the serial code...
//This if statment checks to make sure that the SSN is a valid SSN... if you are willing
// to accept ANY SSN then replace this with 'retVal=RetSSN.hold;'
if (RetSSN.area == 0 || RetSSN.area >=740 || (RetSSN.group==0) || (RetSSN.serial==0))
{
//Invalid SSN according to http://www.usrecordsearch.com/ssn.htm
retVal = 0;
}else
{
retVal=RetSSN.hold;
}
}
return retVal;
}
This post has been edited by NickDMax: 2 Apr, 2007 - 10:39 PM