I have made some changes. Thank you all for your feedback. Now why am I getting an error when trying to access this portion in my code?
Code:
fscanf(fin, "%s ", &player[i].last);
This is my full code:
Code:
#include<stdio.h>
#include<stdlib.h>
#define NUMBERS_PLAYED 6
FILE* fin;
FILE* fout;
int i;
struct players
{
char* last[19];
char* first[19];
int nums_played[6];
int count;
};
enum MATCHED
{
THREE = 10,
FOUR = 1000,
FIVE = 10000,
SIX = 1000000
};
int main (void)
{
char filename[1024];
int winners[6];
int i, j;
int k;
int ticketsbought = 0;
struct player* players;
//Ask user for the name of the file to read from
printf("Please enter the name of the file with the ticket data. \n");
//Read in the file to read from
scanf("%s", filename);
//Open file for reading
fin = fopen(filename, "r");
if(fin == NULL)
{
printf("Unable to open the file %s\n", filename);
system("PAUSE");
return 0;
}
else
printf("File opened successfully!\n\n");
//The first line will contain a single integer n, the total number of
//tickets bought (number of players). Now we will read in that first line
fscanf(fin, "%d ", &ticketsbought);
//Dynamically allocate memory for the number of players in the input file
players = malloc(ticketsbought * sizeof(struct players));
//The first line will contain the last name of the ticket buyer, followed by
//a space, followed by the first name of the ticket buyer
for (i = 0; i < ticketsbought; i++)
{
fscanf(fin, "%s ", player[i].last);
fscanf(fin, "%s ", &player[i].first);
for (j = 1; j <= NUMBERS_PLAYED; j++)
{ fscanf(fin, "%d ", &player[i].nums_played[j]);
}
}
//Ask the user for the winning combination of numbers
printf("Please enter the winning lottery numbers:\n");
scanf("%d %d %d %d %d %d", &winners[0], &winners[1], &winners[2], &winners[3], &winners[4], &winners[5]);
//For debugging purposes only
for (i = 0; i < NUMBERS_PLAYED; i++)
{
printf("%d ", winners[i]);
}
//For each number selected
for (i = 0; i < NUMBERS_PLAYED; i++)
{
//For each player
for (j = 0; j < ticketsbought; j++)
{
//For each number in their array
for (k = 0; k < NUMBERS_PLAYED; k++)
{
//Check to see if it's a winning number
if (player[i].nums_played[j] == winners[k])
{
//Increment the counter for the particular player if a number
//that they played is a match against the winning numbers
player[i].count++;
}
}
}
}
//Final Output
for (i = 0; i < ticketsbought; i++)
{
if (player[i].count >= 3)
printf("%s %s matched %d numbers and won $%d.\n", player[i].first, player[i].last, player[i].count);
}
//Close the input file
fclose(fin);
free(players);
system("PAUSE");
return 0;
}
It is telling me that 'player' is undeclared . Am I saving it to the wrong location? I didn't think I had to separately declare an array of struct players* player 's. Let me know if I'm wrong. Thanks!!