Help formatting text in c

This is a discussion on Help formatting text in c within the C Programming forums, part of the General Programming Boards category; What is a more stable version of \t that will let me indent lines of code without going all over ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    21

    Help formatting text in c

    What is a more stable version of \t that will let me indent lines of code without going all over the place. I need everything line up evenly.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Are you lining up your source code or the output from your program?

    Most IDEs and editors have a "tabs as spaces" feature. You can specify the tab width as well, so your indentation might be 2, 4 or 8 spaces.

    If you're talking about output in your code (e.g. from printf statements), then you need to use the width modifier. Generally, it's an integer between the % and the letter that specifies the type, like %6d or %10f or %-20s. You can use the - for left justification instead of the default right.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    I did what you said anduril but the text is not alligning properly in my case 2 statement.(leaguestandings function)

    Code:
    
    #include <stdio.h>    
    #include <stdlib.h>
     #include <string.h>
    typedef struct {
    char name[20];
            int wins;
            int loses;
            double pct;
            
            }TEAM;
            
    void gameResult(TEAM* home, TEAM* away, int homeScore, int awayScore);
    void calculatePCT(TEAM* team);
    void leagueStandings(TEAM teams[]);
    int getIndex(TEAM teams[], char* teamName);
    
    
    int main()
    {
          //declare variables
          TEAM teams[6];
      
      
       int input=0;
       int homeScore, awayScore, homeIndex, awayIndex;
       char home[20];
       char away[20];
       int i;
       for (i=0;i<6;i++){
           memset(teams[i].name,'\0',20);
       }
       memset(home,'\0',20);
       memset(away,'\0',20);
       sprintf(teams[0].name,"Houston");
       teams[0].wins=0;
       teams[0].loses=0;
      sprintf(teams[1].name,"Milwaukee");
       teams[1].wins=0;
       teams[1].loses=0;
       sprintf(teams[2].name,"StLouis");
       teams[2].wins=0;
       teams[2].loses=0;
      sprintf( teams[3].name,"Cincinatti");
       teams[3].wins=0;
       teams[3].loses=0;
      sprintf( teams[4].name,"Pittsburgh");
       teams[4].wins=0;
       teams[4].loses=0;
      sprintf( teams[5].name,"Chicago");
       teams[5].wins=0;
       teams[5].loses=0;
      
        
                               
    
    
    while(input != 3){
        printf("Sports League Menu\n");
    printf("1) Enter the result of the game\n");
       printf("2) Display the league standings\n");
       printf ("3) Quit the program\n");
       scanf ("%d", &input);
       switch(input){
       
    
    
      case 1:
          fflush(stdin);
         
          printf("Enter the home team's name:\n");
           printf("(Houston, Milwaukee, StLouis, Cincinnati, Pittsburgh, or Chicago\n\n");
          scanf ("%20s", &home);
          printf("Enter the away team's name:\n");
          scanf ("%20s", &away);
          printf("Enter the home score:\n");
          scanf ("%i", &homeScore);
          printf("Enter the away score:");
          scanf ("%i", &awayScore);
          homeIndex = getIndex(teams, home);
          awayIndex = getIndex(teams, away);
          
          if (homeIndex != -1 && awayIndex != -1)
             gameResult(&teams[homeIndex], &teams[awayIndex],homeScore, awayScore);
          else printf("invalid");
          break;
      case 2:
          leagueStandings(teams);
          break;
      case 3:
          printf("\nGoodbye");
          break;
       
      }//end switch
    }//end while
      system ("pause");
       return 0;
     
    }
    void gameResult(TEAM* home, TEAM* away, int homeScore, int awayScore){
         if (homeScore < awayScore){
                  away->wins++;
                  home->loses++;     
         }
         else {
          home->wins++;
          away->loses++;
              }
         
        
    }
    void calculatePCT(TEAM* team){
         int total=(team->wins + team->loses);
             if (total > 0)
         team->pct = team->wins / total;
             else
                 team->pct = 0;
    }
    
    
    
    
    
    
    
    
    int getIndex(TEAM teams[], char* teamName) {
        int result = -1;
        int i;
        
        for (i = 0; i < 6 && result == -1; i++) {
            if (strcmp(teams[i].name, teamName) == 0) {
               result = i;                          
            }    
        }
        
        return result;
    }
    void leagueStandings(TEAM teams[])
    {
        int i;
        printf("Team Name\t\tWins\t\tLoses\t\tPCT\n");
        for (i=0;i<6;i++){
            calculatePCT(&teams[i]);
            printf("%s20%i\t\t%i\t\t%.3lf\n",teams[i].name, teams[i].wins,teams[i].loses,teams[i].pct);
        }
    }
    Last edited by anonymoususer; 11-06-2011 at 10:36 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Code:
    ]$ gcc -g -Wall -o foo foo.c
    foo.c: In function ‘main’:
    foo.c:72:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    foo.c:74:17: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat]
    Take the & off of the variable in the scanf calls on line 72 and 74. The name of the array serves as a pointer to the first element, so you don't need the &.

    For the formatting, you need to put the 20 between the % and the s, or the % and the i, depending on how exactly you want the format. Also, look into using the - character for left justifying: "%-20s" will make the team name left justified and take up 20 spaces.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text Formatting Program Help
    By Larph in forum C Programming
    Replies: 1
    Last Post: 05-02-2011, 08:39 AM
  2. Text Formatting & various questions.
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2005, 08:27 AM
  3. formatting text with cprintf()
    By Noobie in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2003, 05:13 PM
  4. cprintf() formatting text
    By Noobie in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 08:10 PM
  5. need help with formatting text
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2001, 07:52 PM

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