Need help with break in do while loop

This is a discussion on Need help with break in do while loop within the C Programming forums, part of the General Programming Boards category; Hi All, I'm wondering if anyone can tell me where to put the "break" statement in the code below. I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    Need help with break in do while loop

    Hi All,
    I'm wondering if anyone can tell me where to put the "break" statement in the code below. I'm trying to make it so the program will keep asking a user to input a positive integer and then output it's factorial. However, the first iteration gets caught up in a continual loop. For example, instead of outputing the factorial of the integer 4, for example, just once and then prompting the user to input another integer I just get the output such as:

    4! = 24
    4! = 24
    4! = 24
    4! = 24
    4! = 24
    ...etc. continuing on forever.

    Can anyone help? Thank you in advance! Joe

    Code:
    #include <stdio.h>
     
    int main()
    {
    // Program calculates factorial of user inputted integer
     
       long int x,y,result;
     
       x=-1;
    // Controls for negative integer inputted; can only accept positive integer
       printf("Please enter a positive integer:"); 
       scanf("%d",&x);
    
       do {
       
    // Calculates the factorial
       result = 1;
       for (y=x;y>=1;y--)
    	 result = result*y;
       printf("%d! = %d\n",x,result);
       } while (x!=0);
    
    	   printf("Negative integer inputted, Please Try Again!!!\n");
    
       return 0;
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
       do {
       
    // Calculates the factorial
       result = 1;
       for (y=x;y>=1;y--)
    	 result = result*y;
       printf("&#37;d! = %d\n",x,result);
       } while (x!=0);
    x is never going to be 0 unless it's initially entered as 0.

    Consider this,
    * Ask for a number, n
    * multiply n - 1, n - 2, n - 3, ... n - n and put in 'result'
    * print result

    Start you loop at 1 working your way upto n (the integer the user entered) -- remember don't include 0 in the factorial calculation, ie
    Code:
    for(counter = 1; counter < number they entered; counter++)
    {
        /* do calculation with 'counter' */
    }
    Last edited by zacs7; 03-19-2008 at 10:07 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    can you help more?

    HI Zacs7,
    Thanks for the assistance. I'm too much of a beginner to even understand what you're talking about much less where I'd put the counter statement. Can you help more in terms of which mistakes I should correct and where to put a break statement? Thanks again.
    Joe

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You've written code without knowing what it does.
    I think the best thing you can do is to use your debugger to step through the code one line at a time. That will show you exactly what it is doing at each step and you will learn a lot. It should also show you what you are doing wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21