home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11543 < prev    next >
Encoding:
Text File  |  1992-07-23  |  2.7 KB  |  89 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!destroyer!ubc-cs!unixg.ubc.ca!lermer
  3. From: lermer@theory.chem.ubc.ca (Noah Lermer)
  4. Subject: Loop structures
  5. Message-ID: <1992Jul24.003402.9071@unixg.ubc.ca>
  6. Sender: news@unixg.ubc.ca (Usenet News Maintenance)
  7. Nntp-Posting-Host: theory.chem.ubc.ca
  8. Organization: University of British Columbia, Vancouver, B.C., Canada
  9. Date: Fri, 24 Jul 1992 00:34:02 GMT
  10. Lines: 77
  11.  
  12. Hello everyone,
  13.  
  14. I've got a loop flow problem.
  15.  
  16.  I read a value from the user
  17.  Check that the value is valid,
  18.    if it is then I just want to continue,
  19.    if it is not, then I display an error and reprompt for the value.
  20.  
  21. in pseudocode:
  22.  
  23.  :loop
  24.     get input <---------
  25.     if (!valid)         |
  26.       complain to user  |
  27.       then loop: -------
  28.  
  29. The problem comes when I want to code this in C
  30. I've found a couple ways of doing this, none of which I like completely.
  31.  
  32. Solution i)
  33.  
  34. do {
  35.     ... get input ...
  36. } while ( !valid ? showerror(),1 : 0 );
  37.  
  38. The obvious problem with this is that it is ugly and hard to read,
  39. let alone maintain, but could be ok if you macro-ised it.
  40.  
  41. Solution ii)
  42.  
  43. :label
  44.     .... get input ...
  45.     if (!valid) 
  46.     {
  47.         showerror();
  48.         goto label;
  49.     }
  50.  
  51. Aside from the religious issues involved, using a goto in this situation
  52. can also get hairy to look at since I need several inputs one after another
  53. (all with their own set of labels etc...) separated by a fair bit of code
  54. from the goto (the get input section can be fairly long). It gets easy to
  55. loose track of which label goes with which code block, especially if they
  56. are nested (ie, parts of one input are validated individually as well).
  57. On the other hand, it is easily extensible to multiple error conditions.
  58.  
  59. Solution iii)
  60.  
  61. do {
  62.     ... get input ...
  63.     if (valid)
  64.         break;
  65.     showerror();
  66. } while(1);
  67.  
  68. This is the solution I like the most, but has the disadvantage (like i) that
  69. multiple error conditions are not handled easily. This should also be easy to
  70. macro-ise.
  71.  
  72. Some more comments:
  73. The (valid) or (!valid) condition is a rather complicated arithmetic expression
  74. I would prefer to only have to calculate once. At the moment I have only one
  75. error condition, but as a general algorithm, I would prefer to be able to
  76. handle different types of errors. 
  77.  
  78. For what it's worth, the input routines are not from the standard library.
  79. I'm doing direct screen i/o on a PC, so any technique you suggest should not
  80. make any assumptions about <stdio.h>
  81.  
  82. I'd really appreciate any comments/suggestions (/flames :) you
  83. care to send my way. E-mail (hollebon@cemaid.chem.ubc.ca or
  84. lermer@theory.chem.ubc.ca) and I'll summarise for the 'net.
  85.  
  86. Thanks for your time,
  87. Bruce Hollebone,
  88. hollebon@cemaid.chem.ubc.ca
  89.