do while loop keeps looping

This is a discussion on do while loop keeps looping within the C Programming forums, part of the General Programming Boards category; Hi, Im working on a program and im trying to use a do while loop in it. My only issue ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    53

    do while loop keeps looping

    Hi, Im working on a program and im trying to use a do while loop in it. My only issue is that the loop never ends, it keeps going and going and I thought I had it right. Can anyone see a problem with it? The first time the loop goes around and I get the answer I want, but it just keeps spitting out that answer until I force quit the program.

    Code:
    	do 
    		{
    			removed = number2/10;
    			digit = removed % 10;
    			product = digit*number1;
    			printf("+ %d", product);
    		} while (digit != '\0');
    Im not sure if I need to post the rest of the code to diagnose, but let me know if thats needed.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Suppose number2 has the value of 10. On the first iteration of the loop,
    removed = number2 / 20 = 10 / 10 = 1.
    digit = removed % 10 = 1 % 10 = 1
    ...
    digit != '\0' = 1 != 0 = 1
    so we go on to the next iteration. In the next iteration,
    removed = number2 / 20 = 10 / 10 = 1.
    digit = removed % 10 = 1 % 10 = 1
    At this point we can easily conclude that since number2 is never changed, the loop will loop infinitely.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    Suppose number2 has the value of 10. On the first iteration of the loop,
    removed = number2 / 20 = 10 / 10 = 1.
    digit = removed % 10 = 1 % 10 = 1
    ...
    digit != '\0' = 1 != 0 = 1
    so we go on to the next iteration. In the next iteration,
    removed = number2 / 20 = 10 / 10 = 1.
    digit = removed % 10 = 1 % 10 = 1
    At this point we can easily conclude that since number2 is never changed, the loop will loop infinitely.
    Ah I see. That was really stupid of me. So basically I need to give number2 the removed value so it keeps going down by 1 digit each time. Let me give this a shot, thanks laserlight

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by wankel View Post
    Ah I see. That was really stupid of me. So basically I need to give number2 the removed value so it keeps going down by 1 digit each time. Let me give this a shot, thanks laserlight
    Got it working. I just used this:

    Code:
    number2 = number2/10;
    etc
    Everything is working awesome, only problem is that i get a zero at the end. for example:
    1
    x 1234
    --------
    4
    3
    2
    1
    0

    Would there be a more efficient way to loop this so that I dont always get a zero at the end?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> Would there be a more efficient way to loop this so that I dont always get a zero at the end?

    Just test the condition in the middle of the loop, eg:

    Code:
    for( ;; )
    {
    	// calculations
    	if( condition )
    		break;
    	// finalize results	
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Sebastiani View Post
    >> Would there be a more efficient way to loop this so that I dont always get a zero at the end?

    Just test the condition in the middle of the loop, eg:

    Code:
    for( ;; )
    {
    	// calculations
    	if( condition )
    		break;
    	// finalize results	
    }
    I used an if else statement:

    Code:
    if (product = 0)
    			{
    				break;
    			}
    
    			else {
    			printf("+ %d\n", product);
    			}
    but it gives me"

    1
    x1234
    -------
    4
    0

    so it skips everything from the first number until zero. Can I not use an if else statement for this?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Sure you can. You just have to do it properly. Note that = and == do not mean the same thing.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    Sure you can. You just have to do it properly. Note that = and == do not mean the same thing.
    touche. I need to use "==", which means equal to. However, using that, I get this:

    1
    x1234
    -------
    4
    3

    So it just ends at 3, which is weird.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wankel View Post
    touche. I need to use "==", which means equal to. However, using that, I get this:

    1
    x1234
    -------
    4
    3

    So it just ends at 3, which is weird.
    Show your code. It should have a + in front of it, shouldn't it? Or else it's being printed by something later on (just as your 0 wasn't printed by this code, but by your print statements after it).

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    hmm...post the code as it exists now again.

    btw, you don't really need an if...else when the fulfilment of the "if" is a break or a return. You can just use the "if", if you follow the logic there.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    Show your code. It should have a + in front of it, shouldn't it? Or else it's being printed by something later on (just as your 0 wasn't printed by this code, but by your print statements after it).
    It does have the + before it, I was just simplifying it to show whats going on. Heres the full code:


    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int number1;
    	int number2;
    	int sum;
    	int product;
    	int digit;
    	int final_product;
    	int removed;
    
    	printf("Enter first number:");
    	scanf("%d", &number1);
    	printf("Enter second number:");
    	scanf("%d", &number2);
    
    	if (number2 < 10)
    	{
    		final_product=number1*number2;
    		printf("  %d\n", number1);
    		printf("x %d\n", number2);
    		printf("------\n");
    		printf("  %d\n", final_product);
    	}
    
    	else
    	{
    		//number2 = number2/10;
    		digit = number2 % 10;
    		product = digit*number1;
    	        printf("%d\n", product);
    
    		do 
    		{
    			number2 = number2/10;
    			digit = number2 % 10;
    			product = digit*number1;
    			if (product == 0)
    			{
    				break;
    			}
    
    			else {
    			printf("+ %d\n", product);
    			}
    
    		} while (digit = '\0');
    
    	}
    
    //printf("%d", final_product);
    
    return 0;
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    while (digit = '\0')
    hmmmmmmmmmmmmmmmmmmmmm.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    while (digit = '\0')
    hmmmmmmmmmmmmmmmmmmmmm.
    Good catch! Its supposed to be "!=". Everything works great now, thank you so much for all youre help

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    39
    Doesn't look like your finished...still need to print the final product result...which you do not seem to have...

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ninety3gd View Post
    Doesn't look like your finished...still need to print the final product result...which you do not seem to have...
    Sounds like you want his answers... :|

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

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