header

Homework 1

For numeric input, there are basically two kinds of errors: range errors and format errors. The program you wrote for Lab1 dealt with range errors; numeric values that are out of the acceptable range of values. Format errors occur when the string entered by the user cannot be parsed as a double value because it contains invalid characters. For example, it might contain two decimal points or contain a letter of the alphabet.

Unfortunately, the extractor operator can't cope with format errors. Consequently, the application program must extract the input as a string (rather than as a number), convert the string to a numeric representation (checking for format errors), and then check for any possible range errors.

The strtod Function

double strtod(const char* str, char** endptr)

The C function, strtod, returns the double value that corresponds to the parameter str which is an old-fashioned C string (an array of characters terminated by the null character). Consequently, the str parameter is a pointer to the first character in an array of characters. As usual, the const keyword preceding the parameter declaration indicates that the function does not modify the str parameter (it serves as an in parameter).

The second parameter is an out parameter and is the address of another character pointer variable (i.e., the address of another old-fashioned C string). When the function terminates, the actual parameter will point to a substring of str that begins with the first character in str that is NOT a valid character and continues to the end of the string. If there are no invalid characters, then this parameter will point at the terminating null character which represents an empty string.

In this code fragment:

doubleValue = strtod(numericStr.c_str(), &endptr);

Recall that the c_str( ) function of the string class converts the value of a string object to an old-fashioned C string.

If, following the invocation of strtod, endptr points to the null character (*endptr = = '\0') then the numeric conversion was a success; otherwise the conversion failed.

invalidRadiusException Class

Write a class named "invalidRadiusException" whose "what" member function returns an appropriate error message. The default constructor should assign the generic message, "Invalid Radius". The initialization constructor should take an integer parameter and assign one of the following messages:

Properties of a Circle Application

Modify the program you wrote for Lab 1 to read the user input as a string. Use the strtod function and your invalidRadiusException class to trap all possible user-entry errors. Allow the user to keep trying until the input contains no errors. Here is a sample run:

Sample Run

File Submission

Name your program file "Hmwk01.cpp" and submit it to me as an email attachment.