Topical Information

This lab will help you practice with input validation, arrays, C-strings, and libraries. Function overloading might come in handy, as well.

Program Information

When the user is expected to enter a value, that value is often under some kind of constraints from the problem we are solving. For instance, a time must be non-negative, a correlation coefficient must lie between -1 and +1, a menu choice must be one of a particular set of values (menu-dependent). Such input validation is easily done with a while loop:

    prompt & read
    while (!valid)
    {
        error message
        prompt & read
    }

But this can become tedious if you have to redesign it or alter the code EVERY time you must use it. To make things easier, we can simply place this loop in a function for easy re-use.

Upon examining several specific instances, we see that the main things that change are the variable you are entering, the bounds/values to be used, and the prompts/messages printed to the user. All of these can easily be parameterized using a function. The bounds could be passed as arguments. So could the prompts and messages (C-string arguments?). And the variable entered would simply be assigned upon return from the function (the return value)!

You decide to write some generalized input-validation functions. To make them even more re-usable, you'll place them in a library called input_prot (for input protection). You figure that you need to be able to handle the following situations:

By overloading your functions for use with ALL of the major data types (double, long, and char) you'll almost never have to re-write that validation loop again! (You can skip float and short since they are subsets of double and long. You can skip bool as it can't be directly entered.

You'd probably call the char one with a list of "YyNn" ...or some such... and store whether the upper-case of the result was equal to 'Y' or not, right? That is, you'd code something like:

    bool ans;

    ans = toupper(input_protect("\nWould you like to continue?  ",
                                "Please enter Yes or No only!\a\n",
                                "YyNn"))
            == 'Y';

in your main program for a bool entry.)

Of course, to make sure all your validation functions work properly, you'll have to write a driver for this library. (A driver is basically a test application that tests ALL the functions in the library — at least once. See the library notes for more tips.)

Thought Provoking Questions

  1. How can you pass a prompt or error message to a function as an argument?

  2. How do you pass a string to a function? Will the strings need to be changed here? What care do you need to take for these arguments, then?

  3. How do you pass a list of values to a function? (Warning! There could be two [related] answers!) Will the elements need to be changed here? What care do you need to take for these arguments, then?

  4. What other arguments does each function take? Are they changed? What special care should you take with them?

  5. What value is returned by your functions? What type is it and what does it represent?

  6. What care does a caller of your functions have to take with this return value? (Can they immediately assume it is a valid entry?)

  7. How does the compiler distinguish which of your functions is being used for a particular call? (They ALL have the same name, after all...)

  8. How do you protect your library from being circularly included?

  9. What changes are needed in your main application (the test application here) to get it to work with the library? What about the compiling process?

  10. How many files does your library consist of? What are they? Which one(s) do you #include?

This assignment is (Level 2).

Options