Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,495 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,353 people online right now. Registration is fast and FREE... Join Now!




Input mask

 
Reply to this topicStart new topic

Input mask, trying to create an input mask in C++

NyGrrl
1 Apr, 2007 - 12:27 PM
Post #1

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 16


My Contributions
Hello All,

I'm working on creating a class called "Employee" and inside this class I would like to have a field for a SSN. The format of the SSN is xxx-xx-xxxx. Ive searched online and cannot seem to figure out a way to do an input mask. is it possible? Or will i have to jump through hoops for this one?

Any help would be appreciated.

Thanks,
A~
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Input Mask
1 Apr, 2007 - 12:55 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, for the SSN format to be xxx-xx-xxxx I think there will be need of regular expressions though I'm not too sure. Without any code it's unlikely to get any help.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Input Mask
1 Apr, 2007 - 01:03 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
In whay way are you looking to implement an input mask? Is this a GUI application where the user will enter the information in a text box? If so, do you want the input mask to be applied client side (before submission), or simply force the mask once submitted? Or is this a console application?
User is online!Profile CardPM
+Quote Post

NyGrrl
RE: Input Mask
2 Apr, 2007 - 07:36 AM
Post #4

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 16


My Contributions
This is a command line program. Nothing GUI driven. Basically I would request the SSN and then store it into a variable. I could just ask for someone to add the dashes and then store it into a string, and then run a check for dashes in my code? Something to think about. I was just curious if there was an actual library for creating a ssn mask, or an easier way that anyone knows of to do this.

BTW - thank you to all who has replied so far. I appreciate it. This seems like a really helpful site.

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Input Mask
2 Apr, 2007 - 07:47 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
There is no specific library meant for ssn input masks, but creating one would not be difficult. You'd really on have to validate that the correct number of digits are entered, and then you could add the dashes yourself.
User is online!Profile CardPM
+Quote Post

ajwsurfer
RE: Input Mask
2 Apr, 2007 - 08:02 AM
Post #6

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
Well, this is definitly a User Interface operation or a Graphical User Interface operation. The library that is used for this in a windows console program is conio.h. For *nix it is ncurses libraries. Keep in mind that once you start using these libraries there are two things that are true.
1. The program is no longer portable accross different platforms.
2. The cout and cin will not work. However, in this case it is good to set up a file stream and stream any messages that you would like for debuging to go into a log file. This way you can test the operation of the program while it is being written. In *.nix you can use the tail -f logfile.name to watch the program opearation in real time from another console.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Input Mask
2 Apr, 2007 - 08:22 PM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
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
User is offlineProfile CardPM
+Quote Post

NyGrrl
RE: Input Mask
4 Apr, 2007 - 07:51 AM
Post #8

New D.I.C Head
*

Joined: 1 Apr, 2007
Posts: 16


My Contributions
NickDMax - thanks for the code! Very interesting. I've never seen an "inline" use before. this was a very nice learning experience. Thanks again.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 06:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month