Code:
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PROGRAM_NAME_SIZE 50
#define TOTAL_PLAYRERS 19
struct _roster {
int number;
char first[20];
char last[20];
int bats;
int hits;
int walks;
int rbis;
float average;
};
char program_name[PROGRAM_NAME_SIZE + 1] = {0};
void calculate_average (struct _roster *roster);
void print_roster(struct _roster *roster);
int main (int argc, char *argv[])
{
struct _roster roster[TOTAL_PLAYRERS] = {
{0, "Joe", "Shmoe", 3, 1, 0, 0},
{4, "Shoeless", "Joe", 5, 1, 1, 1},
{5, "Joey", "Makrs", 4, 1, 1, 0},
{7, "Slim", "Jim", 1, 0, 0, 0},
{18, "Jay", "Hay", 9, 2, 2, 2},
};
strcpy(program_name, basename(argv[0]));
calculate_average(roster);
// print_roster(roster);
/*
char temp[5] = {0};
puts(" # First Last B H W RBI AVG");
puts("== ===== ==== == == == === ===");
for(int x = 0; x < TOTAL_PLAYRERS; x++)
if(roster[x].first[0] != '\0')
{
sprintf(temp, "%.3f", roster[x].average);
printf("%2d %-10s %-10s %2d %2d %2d %2d %s\n", roster[x].number, roster[x].first, roster[x].last, roster[x].bats, roster[x].hits, roster[x].walks, roster[x].rbis, &temp[1]);
}
*/
exit(EXIT_SUCCESS);
}
void calculate_average (struct _roster *roster)
{
for(int x = 0; x < TOTAL_PLAYRERS; x++)
if(roster[x].first[0] != '\0')
roster[x].average = (float) roster[x].hits / (float) roster[x].bats;
}
void print_roster(struct _roster *roster)
{
char temp[5] = {0};
puts(" # First Last B H W RBI AVG");
puts("== ===== ==== == == == === ===");
for(int x = 0; x < TOTAL_PLAYRERS; x++)
if(roster[x].first[0] != '\0')
{
sprintf(temp, "%.3f", roster[x].average);
printf("%2d %-10s %-10s %2d %2d %2d %2d %s\n", roster[x].number, roster[x].first, roster[x].last, roster[x].bats, roster[x].hits, roster[x].walks, roster[x].rbis, &temp[1]);
}
}
The program has been minimized a bit by removing the reading of the roster file to populate the struct array. Basically, it you run the program as is, it displays the correct info: