Printing 20 lines at a time

This is a discussion on Printing 20 lines at a time within the C Programming forums, part of the General Programming Boards category; I am completing a group project and I just found out that I need to print just 20 lines at ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Printing 20 lines at a time

    I am completing a group project and I just found out that I need to print just 20 lines at a time. I have added the code I thought I needed to add, but it wouldnt work correctly. I know this is easy and I should know how to do it, but I am just having trouble thinking through the process logically. I need to add code to the following piece of code to print only 20 lines at a time.

    for (j = 0; j < i; j++)
    {
    for(k = 0; k < 4; k++)
    printf("\t%10d", CustAry[j][k]);
    printf("\n");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0; x < 20; x++ )
    {
        puts( string );
    }
    fgetc( stdin );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Printing 20 lines at a time

    Originally posted by csmatheng

    Code:
    for (j = 0; j < i; j++) 
       {
       	for(k = 0; k < 4; k++)
          	printf("\t%10d", CustAry[j][k]);
                    printf("\n");
       }
    what is i set to? You should just be able to set it to 20 to print the first 20 elements of the array. But you must check that there are actually 20 or more elements, or you'll prog will fail.

    Also, for clarity, try writing your code with the braces after the inner for loop. It makes it easier to read, especially when you're starting out:

    Code:
    for (j = 0; j < i; j++)
    {
    	for(k = 0; k < 4; k++)
    	{
    		printf("\t%10d", CustAry[j][k]);
    	}
    	printf("\n");
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
    for (j = 0; j < i; j++) 
    { 
       for(k = 0; k < 4; k++) 
          printf("\t%10d", CustAry[j][k]); 
       printf("\n"); 
       if ((j+1)%20 == 0)
       {
          printf("Press <Enter> to continue.");
          getchar();
       }
    }

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Hammer (i is set to # of customers)

    Hammer, " i " is set to the number of customers so I cannot use it to control the loop. I just need to add to the code I posted, so that it will only print 20 lines at a time. Thanks for your help and interest.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Hammer (i is set to # of customers)

    Originally posted by csmatheng
    I just need to add to the code I posted, so that it will only print 20 lines at a time.
    Then it looks like swoopy's code (or a variant of) will do what you want, depending on what you want to do when you get to 20 lines (pause, stop etc).

    If you want to stop at 20, another way to do so is to just change your original for statement a little:

    Code:
    for (j = 0; j < i && j < 20; j++)
    {
    	for(k = 0; k < 4; k++)
    	{
    		printf("\t%10d", CustAry[j][k]);
    	}
    	printf("\n");
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 08:51 PM
  2. Replies: 0
    Last Post: 10-07-2008, 01:09 PM
  3. printing macro's at compile time
    By KIBO in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2007, 09:11 AM
  4. Replies: 1
    Last Post: 06-07-2006, 10:42 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 10:08 AM

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