Thread: Why does the loop in my program turn infinite?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Exclamation Why does the loop in my program turn infinite?

    With no good reason I know..........the loop in here turn infinite
    can anybody know why

    Code:
    #include <stdio.h>
    int main()
    {
    int x,ul,ll,j=1;
    
    printf("Please enter your desired number you want its divisible numbers\n");
    scanf("%i",&x);
    
    
    printf("Please enter the lower limit you desire\n");
    scanf("%i",&ll); //ALERT: getchar(); --> its only with characters!
    
    printf("Please enter the upper limit you desire\n");
    scanf("%i",&ul);
    
    
    	for (j >= ll ; j <= ul; j--)
    	{
    		if (j < x)
    		{
    	if (j % x  == 0)
    			{
    				printf("\n%i", x);
    				j++;
    
    			}
    		}
    		else
    		{       if ( j == x)
    			{
    				j = 0;
    				printf("\t\t");
    			}
    		}
    	}
    
    	return (0);
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    for (j >= ll ; j <= ul; j--)
    You're setting j to be 0 or 1 depending on if it's equal or not to ll. Is this what you want? I think you really want to set it to the lower bound.

    Also, you're going backwards by decrementing the value of j. This means if you start at 0 or 1, it'll go backwards to -1, -2, -3, -4, until underflow will most likely occur and you come back to whatever your upper bound is. Technically, this isn't an infinite loop if underflow occurs, but it might take kind of long as you probably found out. You probably want to from from lower bound to upper bound, so an increment makes more sense.

  3. #3
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    @DaniiChris
    I think this code will work for you!!!!

    Code:
    #include <stdio.h>
    int main()
    {
    int x,ul,ll,j;
    
    printf("Please enter your desired number you want its divisible numbers\n");
    scanf("%i",&x);
    
    
    printf("Please enter the lower limit you desire\n");
    scanf("%i",&ll); //ALERT: getchar(); --> its only with characters!
    
    printf("Please enter the upper limit you desire\n");
    scanf("%i",&ul);
    
    	for (j=0;j<ul;j++)
    	{
    		if((j>ll)&&(x < j))
    		{
    		if (j%x == 0)
    		printf("\n%d\n",j);	
    		}
    		
    	}
    			
    	return 0;
    }

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    int x,ul,ll,j=1;
    Single / two letter variables names are bad enough, but the variable name in red is especially bad. Find descriptive names for your variables - The first time I read your code I read that as "11" not "ll", and in the forum's font, thats:
    Code:
    11
    ll
    ...nearly identical.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not to mention ll stands for "long long",

    ie,
    Code:
    ll = (1298ul*ul) + (148192ll*ll);
    Gets rather confusing, another good reason not to use 'll' or even 'ul' as a variable name
    Last edited by zacs7; 07-07-2008 at 05:43 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, DaniiChris, but after seeing my example in Displaying Numbers divisible by user's desired number , why did you revert to your version? If you do not understand the code, just ask for an explanation.

    If you want to use the brute force method instead of Salem's idea, then fine, but I posted my example partially to give you an idea of what readable code looks like, so try and follow it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Sure lasernight...Thanks a million! You are like my online teacher
    Thanks to all
    MacGyuver and Lasernight !
    I am grateful!
    Thanks to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. infinite loop
    By puckparches in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2005, 10:12 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM
  5. Infinite Loop!!!
    By catmom in forum C++ Programming
    Replies: 9
    Last Post: 12-11-2001, 10:44 AM