home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!mcdd1!mcdd1.uucp!clive
- From: clive@mcdd1.uucp (Clive Backham)
- Newsgroups: comp.lang.c
- Subject: Re: Loop structures
- Message-ID: <385@mx1>
- Date: 27 Jul 92 12:18:31 GMT
- References: <1992Jul24.003402.9071@unixg.ubc.ca>
- Organization: McDonnell Douglas,Hemel Hempstead,UK
- Lines: 42
-
- In article <1992Jul24.003402.9071@unixg.ubc.ca>, lermer@theory.chem.ubc.ca (Noah Lermer) writes:
- > I've got a loop flow problem.
- >
- > I read a value from the user
- > Check that the value is valid,
- > if it is then I just want to continue,
- > if it is not, then I display an error and reprompt for the value.
- >
- > in pseudocode:
- >
- > :loop
- > get input <---------
- > if (!valid) |
- > complain to user |
- > then loop: -------
-
- I'd suggest that you put the <get input, validate> task into a separate
- static function that returns non-zero (ie. true) if the input was
- invalid. Thus:
-
- static int getinput()
- {
- get input;
- if (!valid)
- {
- complain to user;
- return(1);
- }
- else
- return(0);
- }
-
- .....
-
- while(getinput());
- /* and at this point, the input is valid */
-
- Of course, the "getinput" function will need to be passed whatever
- parameters are required to (i) read the I/O device, (ii) validate the
- answer, and (iii) return the valid answer.
-
- - Clive (mcdd1.uucp!clive@uknet.uucp)
-