home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11642 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  1.3 KB

  1. Path: sparky!uunet!mcsun!uknet!mcdd1!mcdd1.uucp!clive
  2. From: clive@mcdd1.uucp (Clive Backham)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Loop structures
  5. Message-ID: <385@mx1>
  6. Date: 27 Jul 92 12:18:31 GMT
  7. References: <1992Jul24.003402.9071@unixg.ubc.ca>
  8. Organization: McDonnell Douglas,Hemel Hempstead,UK
  9. Lines: 42
  10.  
  11. In article <1992Jul24.003402.9071@unixg.ubc.ca>, lermer@theory.chem.ubc.ca (Noah Lermer) writes:
  12. > I've got a loop flow problem.
  13. >  I read a value from the user
  14. >  Check that the value is valid,
  15. >    if it is then I just want to continue,
  16. >    if it is not, then I display an error and reprompt for the value.
  17. > in pseudocode:
  18. >  :loop
  19. >     get input <---------
  20. >     if (!valid)         |
  21. >       complain to user  |
  22. >       then loop: -------
  23.  
  24. I'd suggest that you put the <get input, validate> task into a separate
  25. static function that returns non-zero (ie. true) if the input was
  26. invalid. Thus:
  27.  
  28.     static int getinput()
  29.     {
  30.     get input;
  31.     if (!valid)
  32.     {
  33.         complain to user;
  34.         return(1);
  35.     }
  36.     else
  37.         return(0);
  38.     }
  39.  
  40.     .....
  41.  
  42.     while(getinput());
  43.     /* and at this point, the input is valid */
  44.  
  45. Of course, the "getinput" function will need to be passed whatever 
  46. parameters are required to (i) read the I/O device, (ii) validate the
  47. answer, and (iii) return the valid answer.
  48.  
  49. - Clive    (mcdd1.uucp!clive@uknet.uucp)
  50.