Text Alignment

This is a discussion on Text Alignment within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm working on a programming assignment and I need a little help. In the code below I've had ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Text Alignment

    Hi everyone,
    I'm working on a programming assignment and I need a little help. In the code below I've had to manually type in spaces to get everything to line up. Although this works, it is getting somewhat tedious to make it fit. Is there another way to do this?

    Thanks

    printf("Nonexistent Airlines -- Flights for 21 Sep 2010\n");
    printf("\n");
    printf("Depature Time Arrival Time\n");
    printf("====================================\n");






    return 0;

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    For console output, there '\t' for tab, and field widths for format specifiers in printf() and scanf(). That's as good as it will get for you.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    There's probably an easier way to do it, but I couldn't find it.

    refer to: C-equivalent of the 'setw' function - efreedom which looks for a C version of the std::setw function found in C++. Also refer to Commonly Asked C/c++ 2 - Discuss Anything (point 2.2)

    Code:
    printf("Depature Time%52s\n", "Arrival Time");
    That is my, probably not ideal, solution. Notice that %52s denotes %<number of spaces + length of string>s.

    Someone with a better idea or corrections, please speak!
    Last edited by Jorl17; 09-19-2010 at 05:26 PM.

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Might as well...
    Code:
    printf( "%-20s%20s\n", "Departure Time", "Arrival Time" );

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thiis is the printing code, with the aligned output. Should give you some idea's:

    Code:
      printf("\n\nName                Sex  ID  Ratings\n");
      printf("====================================");
    
      for(i=0;i<count;i++) {
        printf("\n%-19s  %c  %2d ", names[i].name, names[i].sex, names[i].id);
        for(j=0;ratings[i][j]>0;j++) {
          printf("  %d", ratings[i][j]);
        }
      }
    
      fclose(fp);
      free(names);
      printf("\n\n\t\t\t    press enter when ready");
      i=getchar();
      return 0;
    }  
    //Output:
    
    groups: 2  each: 3
    
    Name                Sex  ID  Ratings
    ====================================
    Adam                 M   1   4  8  7
    Bob                  M   2   6  7  5
    Carl                 M   3   5  9  6
    Diana                F   4   7  6  8
    Ellen                F   5   6  5  9
    Fran                 F   6   4  7  3
    
    			    press enter when ready

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Awesome! I'll play around with these suggestions and see if I can get it to work.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  2. RichEditCtrl & unicode formatted text
    By ddonate in forum Windows Programming
    Replies: 0
    Last Post: 03-26-2010, 10:50 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM

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