Thread: Asterisk Histogram - Infinite Loop

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Asterisk Histogram - Infinite Loop

    Hi guys. I wrote a small program where the user inputs a number and the program prints the number in asterisks. I used a for loop for this. However when I put the for loop inside a while loop to keep looping the program, it goes into an infinite loop. I just can't seem to figure out what the problem is.

    Without the while loop the for loop works perfectly, printing out the asterisk(s). It's when I add the while loop that things get ugly.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int number, counter;
    	printf("Enter number: ");
    	scanf("%d", &number);
    
    	while (number > 0){
    		for (counter = 0; counter < number; counter++){
    			printf("*");
    		}
    	}
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep, sure will... because the exit condition is never met. You need to count down your number variable until it hits 0.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Would you care to explain what you mean by count down my number variable?

    I can't use break statement's either. I need to exit this loop, but exit how?
    Last edited by Tony92; 08-01-2011 at 04:26 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Tony92 View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int number, counter;
    	printf("Enter number: ");
    	scanf("%d", &number);
    
    	while (number > 0){
    		for (counter = 0; counter < number; counter++){
    			printf("*");
    		}
    	}
    
    }
    When does the condition in red ever evaluate to false? What is the value of number in your while loop? When does it change?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Ah I see what you are saying. The printf is outside the loop so more user input cannot go through the program am I right?

    When does is it ever false? When number is less than 0
    What is the value of number? Whatever the user inputs
    When does it change? Never I believe.
    Last edited by Tony92; 08-01-2011 at 04:37 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Tony92 View Post
    When does is it ever false? When number is less than 0
    Actually when number is 0 or less, but you get the picture.

    Quote Originally Posted by Tony92 View Post
    What is the value of number? Whatever the user inputs
    ok.

    Quote Originally Posted by Tony92 View Post
    When does it change? Never I believe.
    So if number never changes in your while loop why do you expect your while loop to end? Note you never ask for another number to be inputted in your while loop.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Yeah I figured that out. The thing is if I put my input statements inside my while loop, the first time the program runs it won't run properly. A Do-While loop will work but the outline says I should use a pre-test loop.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So ask the user once before the loop, then again as the last thing inside the loop.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    There we go I fixed it, tell me what you think.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int number = 1, counter;
    	
    	
    	while (number > 0){
            printf("Enter a number: ");
    	    scanf("%d", &number);
    		for (counter = 0; counter < number; counter++){
    			printf("*");
    		}
    
    	}
    
    
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yep, that works too. Hopefully the instructions say not to use a do-while loop so you can see just how stupid it is to do something like this with a pre-test loop. This is exactly when you should use a do-while.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Seems like you got it. I would also recommend that you return an integer at the end of "main()" - typically zero for normal program termination.

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Yeah it was really irritating me, it specifically said to use a pre-test loop and I think you're right it's to show when exactly is the time to use a do-while. Thanks for your help guys.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Quote Originally Posted by Matticus View Post
    Seems like you got it. I would also recommend that you return an integer at the end of "main()" - typically zero for normal program termination.
    Oh yes I almost forgot about that.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. You define main as returning an int correctly, however you forgot to actually return one, e.g. return(0);

    Other than that, good work. Just keep in mind the way you have your loop setup if the user enters zero to quit your program will still evaluate the for-loop before exiting. Not a big problem for this case however loop logic can become tiresome. For example you could have done:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int number, counter;
    	
    	printf("Enter a number: ");
    	scanf("%d", &number);
    	
    	while (number > 0){
    		for (counter = 0; counter < number; counter++){
    			printf("*");
    		}
    		printf("Enter a number: ");
    	             scanf("%d", &number);
    	}
    
    	return (0);
    }
    EDIT: Holy rapid fire posts while I was typing mine up.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    That is true.

    One question the return 0; why did you add brackets around the 0? I was taught that it was only return 0; and not return (0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you tell me why this causes an infinite loop?
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 07-10-2010, 12:21 AM
  2. help with infinite loop please
    By officedog in forum C Programming
    Replies: 4
    Last Post: 11-05-2008, 02:42 PM
  3. infinite loop
    By hlam in forum C Programming
    Replies: 4
    Last Post: 10-29-2008, 01:16 AM
  4. 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
  5. Why does cin>> go into an infinite loop
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2003, 12:48 AM