[prog] validate input

Dominik Schramm dominik.schramm at gmxpro.net
Mon Dec 22 13:00:08 EST 2003


Hi wolf,

Just adding to the already given advice...

Why not try to put the solution into your own words
first, using some non-C++ "syntax"?

At school we used so-called structure charts and at university
we often used pseudo-code. This helps to formulate a solution
using the basic structures of the language (loops, if statements,
etc.) without having to care about the correct syntax yet.
Directly going into the code means you have to solve the problem
and write synractically correct code at the same time.
The approach of blue printing the code first using some other
syntax allows you to eradicate any logical problems with your
solution prior to formulating it in C++.

On second thought, I won't describe pseudocode as I think it
is actually not so good for beginners.  (You'd write code
in a language similar to the target language but without
too much syntactic detail: this may just confuse you.)

One more recommendation before giving you the chart:
As with any other "foreign language": learn to speak formally first.
You'll get acquainted with colloquial speech automatically over time
by "listening" to others.
E.g. always use braces in if-else, for and while statements even
though they may not be necessary;
stick to one "bracing" scheme (see Daniel's response);
don't overuse empty lines, it's like saying "uhm" all the time.
When you become more familiar with the language
you'll learn plenty of ways to tighten your code.

So here's the structure chart:
(Btw. of course you wouldn't normally write the structure chart
on the computer using ASCII "graphics", but rather scribble it
down on a piece of paper you keep handy.)

Here are the initializations for the (input) "counter",
the "ctPass" counter, and the "ctFail" counter.
+------------------------------------------------------+
| set counter to 1                                     |
+------------------------------------------------------+
| set ctPass to 0                                      |
+------------------------------------------------------+
| set ctFail to 0                                      |
+------------------------------------------------------+
| set result to 0                                      |
+------------------------------------------------------+

This is the loop collecting the data:
+------------------------------------------------------+
| while "counter" is at most 10 do                     |
+   +--------------------------------------------------+
|   | say "input 1 for 'passed' or 2 for 'failed'"     |
|   +--------------------------------------------------+
|   | get "result" from user                           |
|   +--------------------------------------------------+
|   |               "result" is 1?                     |
|   | yes:                no:                          |
|   +-------------------+------------------------------+
|   | increase "ctPass" |         "result" is 2?       |
|   | by 1              | yes:           no:           |
|   +-------------------+--------------+---------------+
|   | increase "counter"| increase     | say "invalid  |
|   | by 1              | "ctFail" by 1| input, try    |
|   |                   +--------------+ again!"       |
|   |                   | increase     |               |
|   |                   | "counter"    |               |
|   |                   | by 1         |               |
+---+-------------------+--------------+---------------+

The point is that the overall counter "counter" does not
increase itself (this is a while loop!). So just don't
increase it if the input is invalid. That's what the
"no-no-part" above does. This way you can be certain that
you have 10 valid numbers.

+------------------------------------------------------+
| say "passed: " ctPass                                |
+------------------------------------------------------+
| say "failed: " ctFail                                |
+------------------------------------------------------+
|                 "ctPass" at least 8?                 |
| yes:                       no:                       |
+--------------------------+---------------------------+
| say "Raise tuition"      |                           |
+--------------------------+---------------------------+

For C/C++ this structure chart maps nicely to the correct
syntax. The following code is not commented for brevity,
which doesn't mean that it is self-explanatory; do insert
some comments keeping in mind the responses to the list,
especially Jenn's and Jacinta's:

int main()
{
    int counter = 1;
    int ctPass = 0;
    int ctFail = 0;
    int result = 0;
   
    while (counter <= 10)
    {
        cout << "input 1 for 'passed' or 2 for 'failed'" << endl;
        cin >> input;
   
        if (result == 1)
        {
            ctPass = ctPass + 1;
            counter = counter + 1;
        }
        else
        { // result is 1? no!
            if (result == 2)
            {
                ctFail = ctFail + 1;
                counter = counter + 1;
            }
            else
            { // result is 2? no!
                cout << "invalid input, try again!" << endl;
            }
        }
    }
   
    cout << "Passed: " << ctPass << endl;
    cout << "Failed: " << ctFail << endl;
   
    if (ctPass >= 8)
    {
        cout << "Raise tuition" << endl;
    }
   
    return 0;
}

hope this helps
dominik




More information about the Programming mailing list