home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16950 < prev    next >
Encoding:
Text File  |  1992-11-21  |  3.2 KB  |  95 lines

  1. Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!eli
  2. From: eli@csd4.csd.uwm.edu (Elihu Lubkin)
  3. Newsgroups: comp.lang.c
  4. Subject: confused
  5. Date: 21 Nov 1992 15:02:00 GMT
  6. Organization: University of Wisconsin - Milwaukee
  7. Lines: 84
  8. Message-ID: <1elj18INNnah@uwm.edu>
  9. NNTP-Posting-Host: 129.89.7.4
  10.  
  11. From uwm.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!dsacg3.dsac.dla.mil!dsac.dla.mil!hq.dla.mil!fhi0055 Sat Nov 21 08:08:00 1992
  12. Path: uwm.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!dsacg3.dsac.dla.mil!dsac.dla.mil!hq.dla.mil!fhi0055
  13. From: fhi0055@hq.dla.mil (Marc Poole)
  14. Newsgroups: comp.lang.c,comp.lang.c++,comp.misc
  15. Subject: Confused
  16. Message-ID: <957@hq.dla.mil>
  17. Date: 20 Nov 92 16:07:52 GMT
  18. Followup-To: comp.lang.c,comp.lang.c++,comp.misc
  19. Organization: HQ Defense Logistics Agency/DASC, Cameron Station, VA
  20. Lines: 54
  21. Xref: uwm.edu comp.lang.c:61777 comp.lang.c++:33756 comp.misc:19235
  22.  
  23.  
  24.  The following is a program that I am trying to write.  I can not seem
  25.  how to get the outer loop to work.  The situation is, the inner loop
  26.  will run until it is finished, when it falls through the program should
  27.  prompt for another int input.  If the user inputs 0 then the whole
  28.  program will end.  If the user inputs any other number, the loop will
  29.  continue. 
  30.  
  31.  My problem is I can't seem to contruct the outer loop to end once 0 is
  32.  entered.  I either get an endless loop (with wrong answers the 2nd time
  33.  through) or I get no answer after input of first int and the program
  34.  ends.
  35.  
  36. #include <stdio.h>
  37. main()
  38. {
  39. float answr;
  40. int fact;
  41. int count;
  42.  
  43.  
  44.  
  45. printf ("This program is designed to print the factoral of a number ");
  46. printf ("entered by the user.\n");
  47. printf ("Enter your number: ____\b\b\b\b");
  48.  
  49. -----------------------------------------------------------------------
  50. |   This will work, although it could be made a lot slicker. trl.
  51. |
  52. |   The outer while needs a stop flag, e.g an int done=0 in declarations;
  53. |   while(!done){
  54. |  ******             scanf ("%d",&fact);  *****Must be in outer while too.
  55. |  **check if done:   if(fact==0)done=1; 
  56. |                     else{
  57. |  *******            count = 1;           ****Must be in outer while,or your
  58. |                                            factorials will be multiplied by
  59. |                                            the previously output ones.
  60. |                     rest of your loop;
  61. |
  62. |                          thelma
  63. ------------------------------------------------------------------------------
  64. while 
  65. {
  66.     while (fact > 0)
  67.        {
  68.        count = count * fact--;
  69.        }
  70. answr = count;
  71. printf ("%e", answr);
  72. printf ("\n");
  73. printf ("Enter another number or 0 to quit: ___\b\b\b");
  74. }
  75.  
  76.  
  77. printf ("\n");
  78. }
  79.  
  80.  I left the while statement in the beginning of the outer loop, because
  81.  that's the loop I am trying to use.  It's the coding for that statement
  82.  I can't figure.  Should scanf come after the first while, with it or is
  83.  while not correct at all?
  84.  
  85.  If anyone can help or steer me in right direction please let me know.
  86.  
  87.  Hints and not answers would be greatly appreciated, explanations would
  88.  be appreciated aslo.
  89.  
  90.  mpoole@hq.dla.mil                   |       To some avail
  91.  
  92.    
  93.  
  94.  
  95.