I am making a program that reads football player stats from a file and places it in an array
how ever my program seems to over read the file which has the stats of 17 players and it makes an array of 101 players with the first 17 being alright and the rest being filled in with spaces and random characters and symbols such as spades
can someone help me fix this
here is the player structure and read data function


typedef struct player
{
int number, atmp, yards, TD;
char fname[30];
char lname[30];
char pos[4];
} playerdata;

/* Begin Read Data function */
void readdata(playerdata player[],
int size)
{
FILE *fptr;


if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL)
{ printf("Error: the file cannot be opened\n");
}
else
size;
int i;
for (i = 0; i < size; i++)
{
fscanf(fptr, "%s %s %s %s %s %s %s", &player.number, &player.fname,
&player.lname, &player.pos, &player.atmp, &player.yards,
&player.TD);
}


}

First - that's not a program you posted, or a snippet of code that will run.

Second - you NEED to use CODE tags. Click on the icon in the forum's editor and paste your code, between the tags it provides you.

Third - you didn't give size a value, but you use it to stop the for loop. ??

Fourth - [B]Welcome to the forum, Bkoper![/B] ;)[CODE ] icon in the forum's editor and paste your code, between the tags it provides you.

Third - you didn't give size a value, but you use it to stop the for loop. ??

Fourth - Welcome to the forum, Bkoper! ;)

Here is the whole code , take note that some things are left in it from an alternate version that i am currently modifying, the case 1 of the switch is what i am currently trying to get right, case 2 will constantly display the first quarterback for some reason

/* Bradley Koperski
   CIS 126
   Project 6
*/

/* This program reads football statistics of players on the Chicago Bears from a 
   text files and disblay the desired information via a numbered menu system. 
*/

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

typedef struct player
{
int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char pos[4];     
} playerdata;


int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char wname[60];
char pos[4];
char playerarray[50];

int request; 


/* Begin Read Data function */
void readdata(playerdata player[], 
int size)
{
    FILE *fptr;
 
    
      if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
     else
size = 50;
int i;
for (i = 0; i < size; i++)
{
fscanf(fptr, "%s %s %s %s %s %s %s", &player[i].number, &player[i].fname, 
&player[i].lname, &player[i].pos, &player[i].atmp, &player[i].yards, 
&player[i].TD);  
}


}
/* End Read Data */


int main(void)
{
FILE *fptr;   
    int size;
    playerdata player[size];
     /* open file */
  readdata(player, size);
 
   if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
   else
   
   {
      printf("Enter request \n");
      printf("1 - List all Chicago Bears players stats\n");
      printf("2 - List all Quarter Back stats\n");
      printf("3 - List all Wide Receiver stats\n");
      printf("4 - List all Running back stats\n");
      printf("5 - End of run\nRequest #? ");
      scanf("%d", &request);

while(request!=5)
{ 
 fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, &atmp, 
        &yards, &TD);
 strcpy(wname, lname);
 strcat(wname, ", ");
 strcat(wname, fname);
   /* Begin switch statement */
   switch(request)
   {
   case 1:
      printf("\nAll Chicago Bears Players Stats:\n");
      printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {     
           int i = 0;
           while (i < size)
           {
         
	  		
              printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
	  		i += 1;
	  		
    	}
    	break;
        }
      break;


case 2:
      printf("\nAll Chicago Bears Quarter Back Stats:\n");
       printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
         
           if (strcmp(player[i].pos, "QB") == 0)
            {
           
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
            
              } 
            
             i +=1;
         }
              
      }
      break;
      
   case 3:
      printf("\nAll Chicago Bears Wide Receiver Stats:\n");
	    printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
          while (strcmp(player[i].pos, "WR") == 0)
            {
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
               i += 1;
              } 
            
         }
              
      }
      break;
   case 4:
      printf("\nAll Chicago Bears Running back Stats:\n");
	 printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");		
      while (!feof(fptr))
      {
            if( strcmp(pos, "RB") == 0 ) 
              	printf("%2d %30s %8d %12d %14d \n", number, wname, atmp, yards, TD);
	  		fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, 
              &atmp, &yards, &TD);
       strcpy(wname, lname);
 				strcat(wname, ", ");
 				strcat(wname, fname);
            
      }
      break;
			
} /* End switch */

rewind(fptr); /* return fptr to beginning of file */

		printf("\nRequest #? ");
		scanf("%d", &request);
		
	} /* End while before switch */

         printf("End of run.\n");
	fclose(fptr);
 }
      /* End else */
return(0); 
}

/* End main */

here is the data to test with


6 Jay Cutler QB 111 1483 7
10 Todd Collins QB 10 68 0
12 Caleb Hanie QB 5 55 0
22 Matt Forte RB 90 352 3
29 Chester Taylor RB 44 160 0
6 Jay Cutler RB 16 81 0
23 Devin Hester RB 4 26 0
13 Johnny Knox RB 1 2 0
22 Matt Forte WR 26 303 3
13 Johnny Knox WR 24 478 1
80 Earl Bennett WR 20 211 0
82 Greg Olsen WR 18 222 2
23 Devin Hester WR 18 182 1
29 Chester Taylor WR 10 78 0
19 Devin Aromashodu WR 7 111 0
88 Desmond Clark WR 1 12 0

and as you may have already guessed, i am not an experienced programmer
this is a class assignment

Here is the whole code , take note that some things are left in it from an alternate version that i am currently modifying, the case 1 of the switch is what i am currently trying to get right, case 2 will constantly display the first quarterback for some reason

/* Bradley Koperski
   CIS 126
   Project 6
*/

/* This program reads football statistics of players on the Chicago Bears from a 
   text files and disblay the desired information via a numbered menu system. 
*/

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

typedef struct player
{
int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char pos[4];     
} playerdata;


int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char wname[60];
char pos[4];
char playerarray[50];

int request; 


/* Begin Read Data function */
void readdata(playerdata player[], 
int size)
{
    FILE *fptr;
 
    
      if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
     else
size = 50;
int i;
for (i = 0; i < size; i++)
{
fscanf(fptr, "%s %s %s %s %s %s %s", &player[i].number, &player[i].fname, 
&player[i].lname, &player[i].pos, &player[i].atmp, &player[i].yards, 
&player[i].TD);  
}


}
/* End Read Data */


int main(void)
{
FILE *fptr;   
    int size;
    playerdata player[size];
     /* open file */
  readdata(player, size);
 
   if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
   else
   
   {
      printf("Enter request \n");
      printf("1 - List all Chicago Bears players stats\n");
      printf("2 - List all Quarter Back stats\n");
      printf("3 - List all Wide Receiver stats\n");
      printf("4 - List all Running back stats\n");
      printf("5 - End of run\nRequest #? ");
      scanf("%d", &request);

while(request!=5)
{ 
 fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, &atmp, 
        &yards, &TD);
 strcpy(wname, lname);
 strcat(wname, ", ");
 strcat(wname, fname);
   /* Begin switch statement */
   switch(request)
   {
   case 1:
      printf("\nAll Chicago Bears Players Stats:\n");
      printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {     
           int i = 0;
           while (i < size)
           {
         
	  		
              printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
	  		i += 1;
	  		
    	}
    	break;
        }
      break;


case 2:
      printf("\nAll Chicago Bears Quarter Back Stats:\n");
       printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
         
           if (strcmp(player[i].pos, "QB") == 0)
            {
           
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
            
              } 
            
             i +=1;
         }
              
      }
      break;
      
   case 3:
      printf("\nAll Chicago Bears Wide Receiver Stats:\n");
	    printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
          while (strcmp(player[i].pos, "WR") == 0)
            {
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
               i += 1;
              } 
            
         }
              
      }
      break;
   case 4:
      printf("\nAll Chicago Bears Running back Stats:\n");
	 printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");		
      while (!feof(fptr))
      {
            if( strcmp(pos, "RB") == 0 ) 
              	printf("%2d %30s %8d %12d %14d \n", number, wname, atmp, yards, TD);
	  		fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, 
              &atmp, &yards, &TD);
       strcpy(wname, lname);
 				strcat(wname, ", ");
 				strcat(wname, fname);
            
      }
      break;
			
} /* End switch */

rewind(fptr); /* return fptr to beginning of file */

		printf("\nRequest #? ");
		scanf("%d", &request);
		
	} /* End while before switch */

         printf("End of run.\n");
	fclose(fptr);
 }
      /* End else */
return(0); 
}

/* End main */

here is the data to test with


6 Jay Cutler QB 111 1483 7
10 Todd Collins QB 10 68 0
12 Caleb Hanie QB 5 55 0
22 Matt Forte RB 90 352 3
29 Chester Taylor RB 44 160 0
6 Jay Cutler RB 16 81 0
23 Devin Hester RB 4 26 0
13 Johnny Knox RB 1 2 0
22 Matt Forte WR 26 303 3
13 Johnny Knox WR 24 478 1
80 Earl Bennett WR 20 211 0
82 Greg Olsen WR 18 222 2
23 Devin Hester WR 18 182 1
29 Chester Taylor WR 10 78 0
19 Devin Aromashodu WR 7 111 0
88 Desmond Clark WR 1 12 0

and as you may have already guessed, i am not an experienced programmer
this is a class assignment

Hi koper,

You declared size as int in main function without defined..

Here is the whole code , take note that some things are left in it from an alternate version that i am currently modifying, the case 1 of the switch is what i am currently trying to get right, case 2 will constantly display the first quarterback for some reason

/* Bradley Koperski
   CIS 126
   Project 6
*/

/* This program reads football statistics of players on the Chicago Bears from a 
   text files and disblay the desired information via a numbered menu system. 
*/

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

typedef struct player
{
int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char pos[4];     
} playerdata;


int number, atmp, yards, TD;
char fname[30]; 
char lname[30];
char wname[60];
char pos[4];
char playerarray[50];

int request; 


/* Begin Read Data function */
void readdata(playerdata player[], 
int size)
{
    FILE *fptr;
 
    
      if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
     else
size = 50;
int i;
for (i = 0; i < size; i++)
{
fscanf(fptr, "%s %s %s %s %s %s %s", &player[i].number, &player[i].fname, 
&player[i].lname, &player[i].pos, &player[i].atmp, &player[i].yards, 
&player[i].TD);  
}


}
/* End Read Data */


int main(void)
{
FILE *fptr;   
    int size;
    playerdata player[size];
     /* open file */
  readdata(player, size);
 
   if ((fptr = fopen("C:/Documents and Settings/Owner/Desktop/Jason/C programing/assignment6/assignment6data.txt", "r")) == NULL) 
     { printf("Error: the file cannot be opened\n");
     }
   else
   
   {
      printf("Enter request \n");
      printf("1 - List all Chicago Bears players stats\n");
      printf("2 - List all Quarter Back stats\n");
      printf("3 - List all Wide Receiver stats\n");
      printf("4 - List all Running back stats\n");
      printf("5 - End of run\nRequest #? ");
      scanf("%d", &request);

while(request!=5)
{ 
 fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, &atmp, 
        &yards, &TD);
 strcpy(wname, lname);
 strcat(wname, ", ");
 strcat(wname, fname);
   /* Begin switch statement */
   switch(request)
   {
   case 1:
      printf("\nAll Chicago Bears Players Stats:\n");
      printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {     
           int i = 0;
           while (i < size)
           {
         
	  		
              printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
	  		i += 1;
	  		
    	}
    	break;
        }
      break;


case 2:
      printf("\nAll Chicago Bears Quarter Back Stats:\n");
       printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
         
           if (strcmp(player[i].pos, "QB") == 0)
            {
           
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
            
              } 
            
             i +=1;
         }
              
      }
      break;
      
   case 3:
      printf("\nAll Chicago Bears Wide Receiver Stats:\n");
	    printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");
      while (!feof(fptr))
      {
         int i = 0;
         while (i < size)
         {
          while (strcmp(player[i].pos, "WR") == 0)
            {
            printf(" %2s %15s %14s %8s %12s %14s \n", &player[i].number, 
              &player[i].lname, &player[i].fname, &player[i].atmp, 
              &player[i].yards, &player[i].TD);
               i += 1;
              } 
            
         }
              
      }
      break;
   case 4:
      printf("\nAll Chicago Bears Running back Stats:\n");
	 printf("\n #         Name                     Attempts       Yards      Touchdowns \n \n");		
      while (!feof(fptr))
      {
            if( strcmp(pos, "RB") == 0 ) 
              	printf("%2d %30s %8d %12d %14d \n", number, wname, atmp, yards, TD);
	  		fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, 
              &atmp, &yards, &TD);
       strcpy(wname, lname);
 				strcat(wname, ", ");
 				strcat(wname, fname);
            
      }
      break;
			
} /* End switch */

rewind(fptr); /* return fptr to beginning of file */

		printf("\nRequest #? ");
		scanf("%d", &request);
		
	} /* End while before switch */

         printf("End of run.\n");
	fclose(fptr);
 }
      /* End else */
return(0); 
}

/* End main */

here is the data to test with


6 Jay Cutler QB 111 1483 7
10 Todd Collins QB 10 68 0
12 Caleb Hanie QB 5 55 0
22 Matt Forte RB 90 352 3
29 Chester Taylor RB 44 160 0
6 Jay Cutler RB 16 81 0
23 Devin Hester RB 4 26 0
13 Johnny Knox RB 1 2 0
22 Matt Forte WR 26 303 3
13 Johnny Knox WR 24 478 1
80 Earl Bennett WR 20 211 0
82 Greg Olsen WR 18 222 2
23 Devin Hester WR 18 182 1
29 Chester Taylor WR 10 78 0
19 Devin Aromashodu WR 7 111 0
88 Desmond Clark WR 1 12 0

and as you may have already guessed, i am not an experienced programmer
this is a class assignment

Hi koper,

The below code works fine...just test with your requirments.

The following are some of errors u did in your code

  • You didn't defined value for size in main function but passed in readdata function
  • In switch case you didn't break the while loop after it reached the false condition

Change the value of size depends on your requirements

#include<stdio.h>
#include<string.h>
 
typedef struct player
{
	int number, atmp, yards, TD;
	char fname[30]; 
	char lname[30];
	char pos[4];     
} playerdata;

//#define size sizeof(playerdata);
 
 
char number[3], atmp[4], yards[4], TD[4];
char fname[30]; 
char lname[30];
char wname[60];
char pos[4];
char playerarray[50];
 
int request;

 
 
/* Begin Read Data function */
void readdata(playerdata player[],int size)
{
    FILE *fptr;
 
 
     if ((fptr = fopen("assignment6data.txt", "r")) == NULL) 
	 { 
		 printf("Error: the file cannot be opened\n");
     }
     else
	 {
		int i;
		for (i = 0; i < size; i++)
		{
			fscanf(fptr, "%s %s %s %s %s %s %s", &player[i].number, &player[i].fname, 
			&player[i].lname, &player[i].pos, &player[i].atmp, &player[i].yards, 
			&player[i].TD);			
		}
	  }
 
}
/* End Read Data */
 
 
int main(void)
{
	FILE *fptr;   
    int size=0;
	char ch;
	
     /* open file to find the size of the file*/
	fptr = fopen("assignment6data.txt", "r");
	while (!feof(fptr))
		if('\n' == fgetc(fptr))
			size++;
	fclose(fptr);

	//Fix No of player
	playerdata player[50];
	readdata(player, size);
 
   if ((fptr = fopen("assignment6data.txt", "r")) == NULL) 
   { 
		 printf("Error: the file cannot be opened\n");
   }
   else
 
   {
      printf("Enter request \n");
      printf("1 - List all Chicago Bears players stats\n");
      printf("2 - List all Quarter Back stats\n");
      printf("3 - List all Wide Receiver stats\n");
      printf("4 - List all Running back stats\n");
      printf("5 - End of run\nRequest #? ");
      scanf("%d", &request);
 
	while(request!=5)
	{
		fscanf (fptr, "%d %s %s %s %d %d %d", &number, fname, lname, pos, &atmp,&yards, &TD);
		strcpy(wname, lname);
		strcat(wname, ", ");
		strcat(wname, fname);
	   /* Begin switch statement */
	   switch(request)
	   {
		   case 1:
			  printf("\nAll Chicago Bears Players Stats:\n");
			  puts("\n #         Name                     Attempts       Yards      Touchdowns ");
			  puts("_____________________________________________________________________________");
			  while (!feof(fptr))
			  {     
				   int i = 0;
				   while (i < size)
				   {	 
					  printf(" %2s |%15s |%14s |%8s |%12s |%14s |\n", &player[i].number, 
					  &player[i].lname, &player[i].fname, &player[i].atmp, 
					  &player[i].yards, &player[i].TD);
					  i += 1;	 
    				}
    			break;
			  }
			break;	

			case 2:
				printf("\nAll Chicago Bears Quarter Back Stats:\n");
				puts("\n #         Name                     Attempts       Yards      Touchdowns ");
				puts("_____________________________________________________________________________");
				while (!feof(fptr))
				{
					 int i = 0;
					 while (i++ < size)
					   if (strcmp(player[i].pos, "QB") == 0)
					   {
							printf(" %2s |%15s |%14s |%8s |%12s |%14s |\n", &player[i].number, 
							&player[i].lname, &player[i].fname, &player[i].atmp, 
							&player[i].yards, &player[i].TD);
					   }
					   break;	 
				}
			break;	

			case 3:
				printf("\nAll Chicago Bears Wide Receiver Stats:\n");
				puts("\n #         Name                     Attempts       Yards      Touchdowns ");
				puts("_____________________________________________________________________________");
				while (!feof(fptr))
				{
					int i = 0;
					while (i++ < size)
					{
						if (strcmp(player[i].pos, "WR") == 0)
						{
							printf(" %2s |%15s |%14s |%8s |%12s |%14s |\n", &player[i].number, 
							&player[i].lname, &player[i].fname, &player[i].atmp, 
							&player[i].yards, &player[i].TD);
						}					
					 }
					break;
				}
			break;

			case 4:
				printf("\nAll Chicago Bears Running back Stats:\n");
				puts("\n #         Name                     Attempts       Yards      Touchdowns ");
				puts("_____________________________________________________________________________");
				while (!feof(fptr))
				{
					int i = 0;
					while (i++ < size)
					{
						if (strcmp(player[i].pos, "RB") == 0)
						{
							printf(" %2s |%15s |%14s |%8s |%12s |%14s |\n", &player[i].number, 
							&player[i].lname, &player[i].fname, &player[i].atmp, 
							&player[i].yards, &player[i].TD);
						}					
					 }
					break;
				}
			break;
			default:
				printf("\nU Entered : %d !!!!!!!!!\n\n",request);
				puts("Enter the correct choice !!!!!!!!!!\n\n");
	   } // End switch
	 
		rewind(fptr); // return fptr to beginning of file
		printf("\nRequest #? ");
		scanf("%d", &request);	 
	} // End while before switch 
     printf("End of run.\n");
	fclose(fptr);
 }//End of else
return(0); 
}// End main
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.