+1(805) 568 7317

this exercise will give you experience in using some error prevention techniques of 5151289

This exercise will give you experience in using some error prevention techniques of C++. You can create your own error checks and print error messages, but C++ also provides a way of capturing unforeseen errors. The try-throw-catch blocks are ways to handle errors. We put a try block around some code that might produce a foreseen or unforeseen error. For errors we know to check for, we can “throw” an exception (which basically means send an error to a “catch” block. It is also possible to set up catch blocks for unforeseen errors like dividing by zero or a user entering letters when we are trying to enter actual numbers. This Exercise looks at the first type of error.

We will create a class named Contact that contains the full name, phone number, and email address of a person. We could make a contact list from our Contact objects. We will check that a name starts with a letter. We will check phone numbers for being 10 digits and only containing digits. We will check that the email address has at least one and only one @ sign. Input will come from the keyboard user.

Here is the UML for the class


-fullName : string

-phoneNum: string

-emailAddress : string

+Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor

+setName(string name) : void

+setPhone(string pn) : void

+setEmail(string em) : void

+getName() : string

+getPhone() : string

+getEmail() : string

+printContact() : void         //print out the name, phone numbers and email address

STEP 2 – Create New Project

Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file for the whole project.

STEP 3 – Documentation

Add this documentation to the program:


Program: Contact List

Programmer: Your Name


Purpose: Demonstrate using error checking and throwing errors from your setters to prevent some types of input errors.


Step 4 – Coding the Class Except Setters

In coding the class, the default constructor should set all string variables to “unknown”. We will not use a constructor with parameters this time. The printContact() method should print out the name, phone numbers and email address with titles. Code the getters as usual – return the attribute value. See below for coding the setters.

Step 5 – Coding the setName() method

The setter will check that the name does not start with a number (digit). I know some names end with numbers so let’s just do this simple check. Use a try block. Test that the first letter of the string is not a digit (use the isdigit() function). If this first letter is a digit, throw the first letter. If not (else), go ahead and set the fullName to the parameter. The catch block will catch the character thrown and print an error message saying that names cannot start with a number. So if the error is thrown, the name will remain “unknown”.

Step 6 – Coding the setPhone() method

Put your error checks in a try block. We will check first that the string is 10 characters long (assume no hyphens or spaces). You can use the length function, that is, the length of a string variable named x would be x.length(). If the string is not 10 characters, throw a number such as a 10. Then use a for loop to check if all characters in the string are digits. If you find one that is not a digit, throw a number such as 0. The catch block will catch the number and check it to print the appropriate error message. If you get past the catch block, that means the phone number was OK. If the error is thrown, the phone number will remain “unknown”.

Step 7 – Coding the setEmail() method

In the try block, check each character in the string using the string length function with a for loop. Set up a counter as well and initialize it to zero. When you find an @ sign in the string, increment the count. After the loop, if the count is 1, all is OK. If the count is not equal to one, throw the count; else set the attribute equal to the parameter. The catch block should catch the count. If it is zero, report that email addresses need an @ sign. If it is over one, report that email addresses can only have one @ sign. Thus if an error is thrown, the email address will remain “unknown.”

Step 8 – The main function

In the main function, declare a Contact object. Declare some string variables for the name, phone numbers and email address.

Display prompts to the user to enter their full name, cell phone and email address. Use getline to get the strings into the local variables. Then use the setters and pass the local variables. Finally display the Contact information using printContact().

Test your program with these kinds of errors:

A name that starts with a digit.

A phone number that is too short and too long

A phone number that has some letters in it but is 10 characters.

An email address with no @ sign.

An email address with more than one @ sign.

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon

Order Now