Thread: stuck in loop

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    stuck in loop

    i get my code to print columns and row at the number i enter at prompt put the program never breaks the loop. What am I doing wrong??
    Code:
    #include <stdio.h>
    
    /* function main begins program execution */
    int main()
    {
       int row = 0; /* initialize row */
       int column;   /* define column */
    
       printf( "Enter a number of row to be printed\n" );
       scanf ( "%d", &row );
    
    
       while ( row >= 1 ) { /* loop until row < 1 */
          column = 1;       /* set column to 1 as interation begins */
    
          while ( column <= row ) {               /* loop 10 times */
             printf( "%s", row  ? "*": "*" ); /* output */
             column++; /* increment column */
          } /* end inner while */
    	  printf( "\n" ); /* begin new output line */
    	   
    		 
       } /* end outer while */
    
       return 0; /* indicate program ended successfully */
    
    } /* end function main */

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    ok here is my fix i was overlooking
    Code:
    #include <stdio.h>
    
    /* function main begins program execution */
    int main()
    {
       int row; /* initialize row */
       int column;   /* define column */
       int x;
    
       printf( "Enter a number of row to be printed\n" );
       scanf ( "%d", &x );
    
       row = x;
    
       while ( row >= 1 ) { /* loop until row < 1 */
          column = 1;       /* set column to 1 as interation begins */
    
          while ( column <= row ) {               /* loop 10 times */
             printf( "%s", row  ? "*": "*" ); /* output */
             column++; /* increment column */
          } /* end inner while */
    	  printf( "\n" ); /* begin new output line */
    	  
    	  x--;
    	  if ( x < 1 ) break;
          
    		  
    		 
       } /* end outer while */
    
       return 0; /* indicate program ended successfully */
    
    } /* end function main */

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    column = 1;       /* set column to 1 as interation begins */
    
    while ( column <= row ) {               /* loop 10 times */
    	printf( "%s", row  ? "*": "*" ); /* output */
    	column++; /* increment column */
    } /* end inner while */
    is equivalent to
    Code:
    for(column = 0; column < row; column++)
    {
       printf("*");
    }
    And this
    Code:
    row = x;
    
    while ( row >= 1 ) {      
    	//SOME CODE HERE
    	x--;
    	if ( x < 1 ) break;
    	
    		  
    	
    } /* end outer while */
    is equivalent to
    Code:
    for(row = x; row >= 1; row--)
    {
    	//SOME CODE HERE
    }
    Keep code simple - it is easier to find bugs in it - your bug is that you decrease x
    But the loop should be on row...

    If you made it by puporse than the second loop should be
    Code:
    for(row = x; x >= 1; x--)
    {
    	//SOME CODE HERE
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03: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, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM