Thread: My program's in an infinite loop!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Question My program's in an infinite loop!

    My program is designed to run an input value into 'mux', shift some bits around and such, using bitwise operations, and then give me an output value. It will then print off a line with some values.

    The problem is, when I run the program, it's stuck in an infinite loop which always prints the following:
    Code:
    Input = 0, S = 15, Y = 0
    So please, if you could tell me where I went wrong and what I could do to fix this problem, I'd be quite grateful
    Thanks

    I've included my code below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void mux(input, selector) {
    	int output;
    
    	if (selector == 255) {
    		int input2;
    		input2 = input >> 4;
    		input = input | input2;  /*line 10*/
    	}
    
    	output = input & 15;	
    	printf ("Input = %d, S = %d, Y = %d \n", input, selector, output);
    }
    
    
    main() {
    	int input, i;
    	int selector;  /*line 20*/
    
    	printf("Testing input line A, B = 0 \n");
    	for (i=0; i<=15; i++) {
    		selector = 15;
    		input = i;
    		mux(input, selector);
    	}
    	printf("\n");
    
    	printf("Testing input line A, B = 15 \n");  /*line 30*/
    	for (i=240; i<=255; i++) {
    		selector = 255;
    		input = i;
    		mux(input, selector);
    	}
    	printf("\n");
    
    	printf("Testing input line B, A = 0 \n");
    	for (i=0; i<=240; i+16) {
    		if (i <= 15) {
    			selector = 15;  /*line 40*/
    		}
    		else {
    			selector = 255;
    		}
    		input = i;
    		mux(input, selector);
    	}
    	printf("\n");
    
    	printf("Testing input line B, A = 15 \n");
    	for (i=15; i<=255; i+16) {  /*line 50*/
    		if (i <= 15) {
    			selector = 15;
    		}
    		else {
    			selector = 255;
    		}
    		input = i;
    		mux(input, selector);
    	}
    	printf("\n");
    } /* line 60*/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    for (i=0; i<=240; i+16) {
    should be:
    Code:
    for (i=0; i<=240; i+=16) {
    You made the same mistake for the next loop.
    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

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    Ah, wow, thanks! I would've -never- though to look at those loops to be honest, but now my program runs how it should Thanks again!

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Infinite loops:

    When a program performs as an infinite loop, there are two places one should look.

    The first place should be the loop exit condition. If it is always true and there are no other loop exit control statements as break, or goto statements then the loop will go forever!

    The second place is the loop control variables, with the most common mistake that they are either not updated with every loop iteration, or their update does not evaluate to a false loop exit condition (a bit more tricky to spot...).

    Always check those two locations when facing the dreaded infinite loop!
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM