home *** CD-ROM | disk | FTP | other *** search
Wrap
Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!eli From: eli@csd4.csd.uwm.edu (Elihu Lubkin) Newsgroups: comp.lang.c Subject: confused Date: 21 Nov 1992 15:02:00 GMT Organization: University of Wisconsin - Milwaukee Lines: 84 Message-ID: <1elj18INNnah@uwm.edu> NNTP-Posting-Host: 129.89.7.4 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 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 From: fhi0055@hq.dla.mil (Marc Poole) Newsgroups: comp.lang.c,comp.lang.c++,comp.misc Subject: Confused Message-ID: <957@hq.dla.mil> Date: 20 Nov 92 16:07:52 GMT Followup-To: comp.lang.c,comp.lang.c++,comp.misc Organization: HQ Defense Logistics Agency/DASC, Cameron Station, VA Lines: 54 Xref: uwm.edu comp.lang.c:61777 comp.lang.c++:33756 comp.misc:19235 The following is a program that I am trying to write. I can not seem how to get the outer loop to work. The situation is, the inner loop will run until it is finished, when it falls through the program should prompt for another int input. If the user inputs 0 then the whole program will end. If the user inputs any other number, the loop will continue. My problem is I can't seem to contruct the outer loop to end once 0 is entered. I either get an endless loop (with wrong answers the 2nd time through) or I get no answer after input of first int and the program ends. #include <stdio.h> main() { float answr; int fact; int count; printf ("This program is designed to print the factoral of a number "); printf ("entered by the user.\n"); printf ("Enter your number: ____\b\b\b\b"); ----------------------------------------------------------------------- | This will work, although it could be made a lot slicker. trl. | | The outer while needs a stop flag, e.g an int done=0 in declarations; | while(!done){ | ****** scanf ("%d",&fact); *****Must be in outer while too. | **check if done: if(fact==0)done=1; | else{ | ******* count = 1; ****Must be in outer while,or your | factorials will be multiplied by | the previously output ones. | rest of your loop; | | thelma ------------------------------------------------------------------------------ while { while (fact > 0) { count = count * fact--; } answr = count; printf ("%e", answr); printf ("\n"); printf ("Enter another number or 0 to quit: ___\b\b\b"); } printf ("\n"); } I left the while statement in the beginning of the outer loop, because that's the loop I am trying to use. It's the coding for that statement I can't figure. Should scanf come after the first while, with it or is while not correct at all? If anyone can help or steer me in right direction please let me know. Hints and not answers would be greatly appreciated, explanations would be appreciated aslo. mpoole@hq.dla.mil | To some avail