Hi guys, I'm making a program tha thas to open a file, and then store the information from the file into a structure variable, only I can't get the function to work. I can get the program to work if I put the reading of the file in the main function, but it has to be it's own function. Now from my tests (by inserting print statements in the while loop that reads everything from the file) it prints out the correct values. So maybe the problem is in my other functions accessing that information.

Here's the code of the function that reads everything from the file:

void fillStructure(playerData *currentPlayer, playerData playerList[], int count, int invalid[])
{
     FILE *inp;
     int status;
     inp = fopen("bbstats.txt", "r");     

     status = fscanf(inp, "%s", currentPlayer->name);
     while(status != EOF)
     {
         fscanf(inp, "%d", &currentPlayer->fieldgoals_att);
          fscanf(inp, "%d", &currentPlayer->fieldgoals_made);
          fscanf(inp, "%d", &currentPlayer->freethrows_att);
          fscanf(inp, "%d", &currentPlayer->freethrows_made);
          printf("%d ", currentPlayer->freethrows_made);
          validateData(*currentPlayer, playerList, count, invalid);
          search(playerList, *currentPlayer, count);
          status = fscanf(inp, "%s", currentPlayer->name);
          count++;
     }

}

That print statement there was my test, ignore it.

This is the function that manipulates all the data and puts it into the structure array. I'm thinking the problem might be there, since in the function that's reading everytyhing into the variable seems to be getting the correct stuff.

void validateData(playerData currentPlayer, playerData playerList[], int count, int invalid[])
{
    if((currentPlayer.fieldgoals_made > currentPlayer.fieldgoals_att)               /* IF statement checks the validity of all the input data, */ 
    || (currentPlayer.freethrows_made > currentPlayer.freethrows_att)               /* if there's invalid data, reports an error and sets that */
     || (currentPlayer.fieldgoals_made <=0 && currentPlayer.fieldgoals_made >=100)   /* player's stats to zero so that they won't effect any of */
     || (currentPlayer.fieldgoals_att <=0 && currentPlayer.fieldgoals_att >=100)     /* the calculations. Then the invalid array is used to set */             
    || (currentPlayer.freethrows_made <=0 && currentPlayer.freethrows_made >=100)   /* the player's corresponding value to 1, which is accesed */
     || (currentPlayer.freethrows_att <=0 && currentPlayer.freethrows_att >=100))    /* by the showPlayerData function to tell which stats will */
     {                                                                               /* be shown, depending on the values in the invalid array. */ 
         playerList[count].fieldgoals_att = 0;
          playerList[count].fieldgoals_made = 0;
          playerList[count].freethrows_att = 0;
          playerList[count].freethrows_made = 0;
          invalid[count] = 1;
          printf("Invalid data in line %d.\n\n", count);
     }
     else
     {      
          strcpy(playerList[count].name, currentPlayer.name);
          playerList[count].fieldgoals_att = currentPlayer.fieldgoals_att;
          playerList[count].fieldgoals_made = currentPlayer.fieldgoals_made;
          playerList[count].freethrows_att = currentPlayer.freethrows_att;
          playerList[count].freethrows_made = currentPlayer.freethrows_made;
     }  
}

For further reference, here is the full program, sort of lengthy, but if anyone can help, I sure would apprecieated it, I've got one day:eek:

#include <stdio.h>
#include <string.h>
#define NOT_FOUND -1

/*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 playerList [],int index)
{   
    int fg_made;
     int ft_made;
     int total;
      
     fg_made = playerList[index].fieldgoals_made;
     ft_made = playerList[index].freethrows_made;
    total = (fg_made * 2)+ ft_made;
     printf("   %d\n\n", total); 
}

/*Computes and displays the fieldgoal percentage*/

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

     field_att = playerList[index].fieldgoals_att;
     field_made = playerList[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 playerList [], int index)
{
    int free_att;
     int free_made;
     double free_percent;
     
     free_att = playerList[index].freethrows_att;
     free_made = playerList[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 playerList [], 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 + playerList[index].fieldgoals_att;
          total_field_made = total_field_made + playerList[index].fieldgoals_made;
          total_free_att = total_free_att + playerList[index].freethrows_att;
          total_free_made = total_free_made + playerList[index].freethrows_made;
     }
    
     if(total_field_att == 0)
     {
         team_field_percent = 0;
     }
     else
     {
         team_field_percent = (double)total_field_made / total_field_att;
     }
     if(total_free_att == 0)
     {
         team_free_percent = 0;
     }
     else
     {
         team_free_percent = (double)total_free_made / total_free_att;
     }
     printf("              Team Avg %.2f        Team Avg %.2f", team_field_percent, team_free_percent);
     
}

/*Combines data of a player who has more than one entry in the data file*/


int search(playerData playerList[],  /* input - array to search    */
       playerData currentPlayer, /* input - value searched for    */
       int    count)      /* input - number of elements to search    */
{
      int i = 0,
      found = 0,    /*  whether or not target has been found     */
      where;          /*  index where target found or NOT_FOUND    */

      /*  Compares each element to target    */
      
    while(!found && i < count)
    {
        if(strcmp(playerList[i].name, currentPlayer.name) == 0)
              {found = 1;}   
          else
             {i++;}   
     }   
     
    /* Returns index of element matching target or NOT_FOUND    */
    if (found)
        {where = i;}          
    else
        {where = NOT_FOUND;}
          
     return (where);
}

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

void validateData(playerData currentPlayer, playerData playerList[], int count, int invalid[])
{
    if((currentPlayer.fieldgoals_made > currentPlayer.fieldgoals_att)               /* IF statement checks the validity of all the input data, */ 
    || (currentPlayer.freethrows_made > currentPlayer.freethrows_att)               /* if there's invalid data, reports an error and sets that */
     || (currentPlayer.fieldgoals_made <=0 && currentPlayer.fieldgoals_made >=100)   /* player's stats to zero so that they won't effect any of */
     || (currentPlayer.fieldgoals_att <=0 && currentPlayer.fieldgoals_att >=100)     /* the calculations. Then the invalid array is used to set */             
    || (currentPlayer.freethrows_made <=0 && currentPlayer.freethrows_made >=100)   /* the player's corresponding value to 1, which is accesed */
     || (currentPlayer.freethrows_att <=0 && currentPlayer.freethrows_att >=100))    /* by the showPlayerData function to tell which stats will */
     {                                                                               /* be shown, depending on the values in the invalid array. */ 
         playerList[count].fieldgoals_att = 0;
          playerList[count].fieldgoals_made = 0;
          playerList[count].freethrows_att = 0;
          playerList[count].freethrows_made = 0;
          invalid[count] = 1;
          printf("Invalid data in line %d.\n\n", count);
     }
     else
     {      
          strcpy(playerList[count].name, currentPlayer.name);
          playerList[count].fieldgoals_att = currentPlayer.fieldgoals_att;
          playerList[count].fieldgoals_made = currentPlayer.fieldgoals_made;
          playerList[count].freethrows_att = currentPlayer.freethrows_att;
          playerList[count].freethrows_made = currentPlayer.freethrows_made;
     }  
}

/*Calls the computation sub funtctions and displays the player's full data*/
     
void showPlayerData(playerData playerList[], int count, int invalid[])
{
    int i;
     printf("                   Daring Dunkers Player Statistics\n\n");
     printf("Player   Field Goals            Free Throws               Total\n\n");
     printf("         Tried  Made    %%       Tried  Made    %%          Points\n\n");

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

/*Reads the data file and fills the structure variable*/

void fillStructure(playerData *currentPlayer, playerData playerList[], int count, int invalid[])
{
     FILE *inp;
     int status;
     inp = fopen("bbstats.txt", "r");     

     status = fscanf(inp, "%s", currentPlayer->name);
     while(status != EOF)
     {
         fscanf(inp, "%d", &currentPlayer->fieldgoals_att);
          fscanf(inp, "%d", &currentPlayer->fieldgoals_made);
          fscanf(inp, "%d", &currentPlayer->freethrows_att);
          fscanf(inp, "%d", &currentPlayer->freethrows_made);
          printf("%d ", currentPlayer->freethrows_made);
          validateData(*currentPlayer, playerList, count, invalid);
          search(playerList, *currentPlayer, count);
          status = fscanf(inp, "%s", currentPlayer->name);
          count++;
     }

}


/*Start of Main Function*/

int main (void)
{
     FILE *starting;
     int count;
     int invalid[1000] = {0};
     playerData playerList[1000];
     playerData    currentPlayer;
     count = 1; 
     starting = fopen("bbstats.txt", "r");
     
     if(starting == NULL)
     {
         printf("Could not open file");
     }    
     else
     {
          fillStructure(&currentPlayer, playerList, count, invalid); 
         showPlayerData(playerList, count, invalid);
         totalAveragePercent(playerList, count);
     } 
    return (0);
}

Debugging a program is almost always the hardest part.

Since you never use the file pointer in main() you can eliminate it.

Since currentplayer isn't passed to showPlayerData() or totalAverage() it can be declared local to fillStructure() and passed to ValidateData(). I don't see where results of search() is ever used so it might as well be removed from the program as well, unless you plan redesign things later. In that case you can comment it out while you figure out the rest of it.

To make sure ValidateData() is getting currentPlayer print out the data before running the if/else and pause the program to study the results if you want. Also print out the playerList[count] after currentPlayer data has been transfered to playerList[count] to be sure it has transfered appropriately.

Keep going from function to function to be sure input and output are what you want or until you find the problem.

This article has been dead for over six months. Start a new discussion instead.