CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2011
    Posts
    41

    problem with printf in loop

    I am having trouble with the printf in this function:
    Code:
    void print_orig_array(char string_array[MAX_PEOPLE][NAME_SIZE], int ages[MAX_PEOPLE], int length)
     {
     	int counter;
     	printf("Original list");
     	printf("\n___________________________\n");
     	for(counter = 0; counter < length; counter++)
     	{
     		printf("%s\t\t\t\t%2d\n", string_array[counter], ages[counter]);
     	}
     	
     }/* end of print_orig_array() */
    Here is the output:
    Name:  ScrShot.png
Views: 310
Size:  3.0 KB

    Am I missing something with the format specifier? How do I fix the 84 that gets pushed out?
    Thanks in advance!

  2. #2
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: problem with printf in loop

    Hi.

    Yo can add more tabs:

    Code:
    printf("%s\t\t\t\t\t\t%2d\n", string_array[counter], ages[counter]);
    or you can add spaces to fit to NAME_SIZE:

    Code:
    void print_orig_array(char string_array[MAX_PEOPLE][NAME_SIZE], int ages[MAX_PEOPLE], int length)
     {
        int counter;
        printf("Original list");
        printf("\n___________________________\n");
        for(counter = 0; counter < length; counter++)
        {
          char name[NAME_SIZE];
          int i=0;
          int len = strlen(string_array[counter]);
          while(i<len) {
             name[i] = string_array[counter];
             i++;
          }
          while(i<(NAME_SIZE-1)) {
             name[i] = ' ';
             i++;
          }
          name[i] = '\0';
           printf("%s\t%2d\n", name, ages[counter]);
        }
        
     }/* end of print_orig_array() */
    Best regards

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: problem with printf in loop

    Quote Originally Posted by juanpast View Post
    Yo can add more tabs:

    Code:
    printf("%s\t\t\t\t\t\t%2d\n", string_array[counter], ages[counter]);
    I don't see how that should help.

    or you can add spaces to fit to NAME_SIZE:

    Code:
    void print_orig_array(char string_array[MAX_PEOPLE][NAME_SIZE], int ages[MAX_PEOPLE], int length)
     {
        int counter;
        printf("Original list");
        printf("\n___________________________\n");
        for(counter = 0; counter < length; counter++)
        {
          char name[NAME_SIZE];
          int i=0;
          int len = strlen(string_array[counter]);
          while(i<len) {
             name[i] = string_array[counter];
             i++;
          }
          while(i<(NAME_SIZE-1)) {
             name[i] = ' ';
             i++;
          }
          name[i] = '\0';
           printf("%s\t%2d\n", name, ages[counter]);
        }
        
     }/* end of print_orig_array() */
    That would help (except for a small error that makes the code fail to compile), but an equivalent effect can be achieved much simpler by specifying a field width for the string:

    Code:
        printf("%-47s %2d\n", string_array[counter], ages[counter]);
    The trick is to specify the field width as a negative number to left-justify the string in the output.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured