Can someone help me with my code? It compiles fine but when i run it it crashes after i input my lottery numbers. Im supposed to write a program that takes a input file with names and 6 numbers 1-50 and then the user is asked to input 6 random numbers to see if anyone won the lotttery. Heres my code
#include<stdio.h>
#include<stdlib.h>
#define NUMBERS_PLAYED 6
FILE* fin;
FILE* fout;
int i, n ,numMatched;
int numsplayed;
struct players
{
char* last[19];
char* first[19];
int nums_played[6];
};
enum MATCHED
{
Win0 = 0,
Win3 = 10,
Win4 = 1000,
Win5 = 10000,
Win6 = 1000000
};
int main (void)
{
char filename[1024];
int winners[6];
int i, j;
int k;
int* ptr;
int ticketsbought = 0;
//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. Now we will read in that first line
fscanf(fin, "%d ", &ticketsbought);
struct players player[ticketsbought];
//Dynamically allocate memory for the number of players in the input file
ptr = (int*)calloc(ticketsbought, sizeof(int));
//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);
// printf("%s ", player[i].last);
fscanf(fin, "%s ", &player[i].first);
// printf("%s \n", player[i].first);
for (j = 1; j <= NUMBERS_PLAYED; j++)
{ fscanf(fin, "%d ", &player[i].nums_played[j]);
// printf("%d ", player[i].nums_played[j]);
}
// printf("\n");
}
//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 (i = 0; i < j; i++)
{
for (j = 0; j < NUMBERS_PLAYED; j++)
{
for (k = 0; k < NUMBERS_PLAYED; k++)
{
if (player[i].nums_played[j] == winners[k])
{
// count++;
}
}
}
}
*/
//int checkMatches(int* winners[i], int* winning)
int numMatched = 0;
{
for(i = 0; n<6; n++)
{
for(j = 0; i<6; i++)//player has 6 numbers to check against each winning number
{
for(k =0; 0<6;k++)
{
if(player[i].nums_played[j] == (winners[k])) numMatched++;
}
}
}
return numMatched;
}
int prize;
if( numMatched == 3) //Checks how many numbers matched and how much money won
prize = Win3;
else if( numMatched == 4)
prize = Win4;
else if(numMatched == 5)
prize = Win5;
else if( numMatched == 6)
prize = Win6;
else
prize = Win0;
printf(" matched %d and won $%f\n", numMatched, prize);
//Close the input file
fclose(fin);
system("PAUSE");
return 0;
}