Ok, I'm doing a basic structure program that displays things. Here's an example of what it should display.

Invalid data in line 11
                  Daring Dunkers Player Statistics

 Player        Field Goals             Free Throws             Total

               Tried  Made    %        Tried  Made    %        Points
Jerry             2     1    0.50         2     1    0.50         3
Akeem             0     0    0.00         0     0    0.00         0
Larry             2     2    1.00         2     2    1.00         6
Julius           25    15    0.60        12     8    0.67        38
WiltTheStilt     83    66    0.80        17    14    0.82       146
Obi              20    13    0.65         7     6    0.86        32
LightningGeorge   4     1    0.25         3     2    0.67         4
PistolPete       25    20    0.80        14    12    0.86        52

                    Team Avg 0.73           Team Avg 0.79

Recommended Answers

All 4 Replies

Here is my program so far.

I'm almost finished.

#include <stdio.h>
#include <string.h>

/*Structure containing all data for each player*/

typedef struct {
    char name[30];
     int fieldgoals_att;
     int fieldgoals_made;
     int freethrows_att;
     int freethrows_made;
     } playerData;

/*Computes and displays Total Points for Each Player*/

void totalPoints(playerData player [],int index)
{   
    int fg_made;
     int ft_made;
     int total;
      
     fg_made = player[index].fieldgoals_made;
     ft_made = player[index].freethrows_made;
    total = (fg_made * 2)+ ft_made;
     printf("   %d\n\n", total); 
}

/*Computes and displays the fieldgoal percentage*/

void fieldPercent(playerData player [], int index)
{
    int field_att;  
     int field_made;
     double field_percent;

     field_att = player[index].fieldgoals_att;
     field_made = player[index].fieldgoals_made;
     if(field_att == 0)
     {
         printf("0\t");
     }
     else
     {
         field_percent = (double)field_made / field_att;
         printf("%.2f\t", field_percent);
     }
}

/*Computes and displays the freethrow percentage*/

void freePercent(playerData player [], int index)
{
    int free_att;
     int free_made;
     double free_percent;
     
     free_att = player[index].freethrows_att;
     free_made = player[index].freethrows_made;
     if(free_att == 0)
     {
         printf("0\t");
     }
     else
     {
         free_percent = (double)free_made / free_att;
         printf("%.2f\t", free_percent);
     }
}

/*Computes the average percentage of fieldgoals and freethrows for the whole team*/

void totalAveragePercent(playerData player [], int count)
{
    int index, total_field_att, total_field_made, total_free_att, total_free_made;
     double team_field_percent, team_free_percent;
     total_field_att = 0;
     total_field_made = 0;
     total_free_att = 0;
     total_free_made = 0;
     
     for(index = 1; index <= count-1; index++)
     {
         total_field_att = total_field_att + player[index].fieldgoals_att;
          total_field_made = total_field_made + player[index].fieldgoals_made;
          total_free_att = total_free_att + player[index].freethrows_att;
          total_free_made = total_free_made + player[index].freethrows_made;
     }
    
     team_field_percent = (double)total_field_made / total_field_att;
     team_free_percent = (double)total_free_made / total_free_att;
     printf("              Team Avg %.2f        Team Avg %.2f", team_field_percent, team_free_percent);
     
}

/*Checks for validity of all data in the data file.*/

int validateData(playerData player[], int count, int invalid)
{
    for(invalid = 0; invalid <= count-1; invalid++)
     {
         if((player[invalid].fieldgoals_made > player[invalid].fieldgoals_att)               /* IF statement checks the validity  */ 
        || (player[invalid].freethrows_made > player[invalid].freethrows_att)               /* of all the input data, reports an */
         || (player[invalid].fieldgoals_made <=0 && player[invalid].fieldgoals_made >=100)   /* error if there's invalid data and */
         || (player[invalid].fieldgoals_att <=0 && player[invalid].fieldgoals_att >=100)     /* skipps that player.               */             
        || (player[invalid].freethrows_made <=0 && player[invalid].freethrows_made >=100)      
         || (player[invalid].freethrows_att <=0 && player[invalid].freethrows_att >=100))
         {
             player[invalid].fieldgoals_att = 
              player[invalid].fieldgoals_made = 0;
              player[invalid].freethrows_att = 0;
              player[invalid].freethrows_made = 0;
              printf("Invalid data in line %d.", invalid);        
         }
    }
     return(invalid);
}

/*Displays the player's full data*/
     
void showPlayerData(playerData player[], int count, int invalid)
{
     int i;
     i = 1;

         for(i != invalid; i <= count-1; i++)
          {  
              printf("%s\t", player[i].name);      
                printf("  %d\t", player[i].fieldgoals_att);
             printf("%d\t", player[i].fieldgoals_made);
              fieldPercent(player, i);
              printf("%d\t", player[i].freethrows_att);
              printf("%d\t", player[i].freethrows_made);
              freePercent(player, i);
              totalPoints(player,i);
                
     }
}


/*Start of Main Function*/

int main (void)
{
     FILE *inp;
     int count, status, invalid;
     playerData player[1000];    
     count = 1; 
     inp = fopen("bbstats.txt", "r");
     
     if(inp == NULL)
     {
         printf("Could not open file");
     }
     else
     {
         status = fscanf(inp, "%s", player[count].name);
         while(status != EOF)
         {
             fscanf(inp, "%d", &player[count].fieldgoals_att);
              fscanf(inp, "%d", &player[count].fieldgoals_made);
              fscanf(inp, "%d", &player[count].freethrows_att);
              fscanf(inp, "%d", &player[count].freethrows_made);
              status = fscanf(inp, "%s", player[count+1].name);
              count++;
         }
        invalid = validateData(player, count, invalid);
         printf("                   Daring Dunkers Player Statistics\n\n");
         printf("Player   Field Goals            Free Throws               Total\n\n");
         printf("         Tried  Made    %%       Tried  Made    %%          Points\n\n");
         showPlayerData(player, count, invalid);
         totalAveragePercent(player, count);
     } 
    return (0);
}

Okay now, my first problem is involves these parts:

int validateData(playerData player[], int count, int invalid)
{
    for(invalid = 0; invalid <= count-1; invalid++)
     {
         if((player[invalid].fieldgoals_made > player[invalid].fieldgoals_att)               /* IF statement checks the validity  */ 
        || (player[invalid].freethrows_made > player[invalid].freethrows_att)               /* of all the input data, reports an */
         || (player[invalid].fieldgoals_made <=0 && player[invalid].fieldgoals_made >=100)   /* error if there's invalid data and */
         || (player[invalid].fieldgoals_att <=0 && player[invalid].fieldgoals_att >=100)     /* skipps that player.               */             
        || (player[invalid].freethrows_made <=0 && player[invalid].freethrows_made >=100)      
         || (player[invalid].freethrows_att <=0 && player[invalid].freethrows_att >=100))
         {
             player[invalid].fieldgoals_att = 
              player[invalid].fieldgoals_made = 0;
              player[invalid].freethrows_att = 0;
              player[invalid].freethrows_made = 0;
              printf("Invalid data in line %d.", invalid);        
         }
    }
     return(invalid);
}

Basically what that huge if statement does is it makes sure all the data in the file is valid, if not, it will display an invalid message and return the invalid intiger.

Next is this function that displays things. It's only supposed to display players who have all valid data. So this means it should skip the number stored in "invalid" I can't figure out how to do that, I was trying a nested while loop under the for loop but that just gave me an infinite loop.

Any tips?

This is the display function. The way I have it now doesn't work, but you get the idea.

void showPlayerData(playerData player[], int count, int invalid)
{
     int i;
     i = 1;

         for(i != invalid; i <= count-1; i++)
          {  
              printf("%s\t", player[i].name);      
                printf("  %d\t", player[i].fieldgoals_att);
             printf("%d\t", player[i].fieldgoals_made);
              fieldPercent(player, i);
              printf("%d\t", player[i].freethrows_att);
              printf("%d\t", player[i].freethrows_made);
              freePercent(player, i);
              totalPoints(player,i);
                
     }
}

Also, I was wondering if anyone nows how to make this function work. I'm trying to make a function that stores everything into the structure array instead of having it all in the main function. It needs to return the count number because all my other functions rely on that.

int fillStructure(playerData player[], int count)
{
    FILE *inp;
     int status;    
     count = 1;
     inp = fopen("bbstats.dat", "r");
     
     if(inp == NULL)
     {
         printf("Could not open file");
     }
     else
     {
        status = fscanf(inp, "%s", player[count].name);
         while(status != EOF)
         {     
                fscanf(inp, "%d", &player[count].fieldgoals_att);
              fscanf(inp, "%d", &player[count].fieldgoals_made);
              fscanf(inp, "%d", &player[count].freethrows_att);
              fscanf(inp, "%d", &player[count].freethrows_made);
                status = fscanf(inp, "%s", player[count+1].name);
              count++;
         }
    
         printf("                   Daring Dunkers Player Statistics\n\n");
         printf("Player   Field Goals            Free Throws               Total\n\n");
         printf("         Tried  Made    %%       Tried  Made    %%          Points\n\n");
     }
     fclose(inp);
     return(count);
}

It just gives me a run time error if I try to run it like that

Your whole concept of arrays is missing some key ideas. Simply using the variable invalid does not indicate invalid data.
If invalid = 2 and valid = 2 then player[valid].name is just player[2].name. You might want to add an invalid key in your structure and keep track using player[x].invalid

And for

i = 1;
for(i != invalid; i <= count-1; i++)

you need to look up the format of the for statement. The first parameter as you have used it does nothing. And the loop itself will simply loop from 0 to count-1 irregardless of the data's validity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.