CS 12 - Lab 5

In this lab you are going to get some practice writing a class.

Note that the program must be written, compiled, and executed under Linux.

Create a directory called lab5 using the mkdir command and create your program files in this directory. Remember, in order to compile your program, you will need to use the g++ command.

The Program

Write a class SSN that maintains a Social Security Number (SSN). A SSN is too long to fit into a single integer variable, so the class should use three integer variables. There are also advantages to storing each part of a SSN separately (instructors often use the last 4 digits, for example).

Definition

Create a C++ header file called ssn.h that contains the definition for your class. The class should contain 3 private integer variables (i.e. data members). The first variable holds the value of the first 3 digits of a SSN, the second holds the value of the next 2 digits, and the third holds the value of the last 4 digits.

In the public section, the class should define a constructor. It takes no parameters and simply sets all three of the private variables to 0.

The public section should also define the following methods:

bool setSSN( char *ssn )
This method takes a string parameter and sets the SSN appropriately. The SSN may or may not contain dashes. For example, both "565-90-2321" and "565902321" are valid. If the ssn is valid, the three data members should be set appropriately and the method should return true. Given the above example, the data members in the class would be set to 565, 90, and 2321. If the ssn is not valid, the method should do nothing but return false.

A valid SSN is a 9-digit number (but remember it might contain dashes). For example, the following are valid SSNs: "555-66-5555", "555665555", "055441234". The following are not valid SSNs: "55-66-5555", "555ff6666", 0055-44-123a".

You can use the function isdigit to determine whether a single character is a digit or not. Its prototype is:

   int isdigit( char c );
The function returns 0 if the character is not a digit and a non-0 value if it is. Note that you must include the ctype.h to use this function.

A string is being passed to the setSSN method, but you need to save integer values. To convert a string value to an integer, you can use the atoi function. The prototype for this function is:

   int atoi( const char *ptr );
It takes a single string parameter and converts it to an equivalent integer. For example, if the string "565" is passed, it returns the integer value 565. Note that you must include stdlib.h to use this function.
void print()
This method prints the SSN, adding dashes so it looks appropriate. Remember to make sure it prints the correct number of digits for each part. (Try "000-00-0000" to make sure it works correctly.)
int getLastFour()
This method simply returns the integer value of the last 4 digits of the SSN.

Implementation

The implementation of the SSN class should be written in the file ssn.cpp. Remember to include "ssn.h" at the top of the file.

Finally, write a main program, in the file main.cpp, that keeps a list of 10 SSN's. Remember to include "ssn.h" at the top of this file too. You will need to declare an array such as:

   const int MAX_SSNS = 10;

   SSN ssns[ MAX_SSNS ]; 
In a loop, the program should ask the user to enter in each SSN, read it in as an array of characters, and call the setSSN method on the appropriate ssns array element to set its value. If setSSN returns false, a message should be printed saying an invalid SSN was entered and the SSN should be re-entered.

After all 10 SSNs have been correctly entered, print the entire list, one to a line.

Finally, search the array for a SSN with a specified last 4 digits. Ask the user to enter a four-digit number (as an integer), then search the list and print all of the SSNs with the same last 4 digits. (For each array element, you can call the getLastFour method to get the value to compare against the value the user entered.)

Grading

When you have finished the program, your TA will grade it. To receive full credit, your TA will check all of the following: