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;   
}

Recommended Answers

All 10 Replies

what compiler are you using? Most compilers will consider line 58 an error and C compilers will not let you declare variables anywhere except at the beginning of a block surrounded by { and }.

line 67: That is the first place where your program crashes. Its because the players struct just decares pointers for the first and last names. Remove the * from those structure items so that the structure will contain character arrays instead of pointers.

Im using the Dev C++ compiler, and im wriitng the code in C

Whether its C or C++ depends on the file extension. *.cpp is compiled as a c++ program and *.c is compiled as a C program.

Remove the * in the structure as I previously mentioned. And it compiles without error as *.c file using Code::Blocks, which is a newest versin of the compiler used with Dev-C++.

Hi,
I compiled your program with Turbo C, I know it is an old compiler but it gave me two errors which supports Ancient Dragon's answer:

Error in line 25: The value of 'Win6' is not within the range of an int
Error in line 58: Constant expression required

I cleared up my code a bit and i got it to compile and run now. But the problem is the output is wrong. The name doesnt appear and it says each person has the same amount of numbers correct and same amount of money. I just have no clue. Thank you for your help, heres my code to look at

#include<stdio.h>
#include<stdlib.h>

#define NUMBERS_PLAYED 6

FILE* ifp;
int i;

struct players
{
    char last[19];
    char first[19];
    int nums_played[6];
    int count;
};

enum MATCHED
{
    NONE = 0,
    THREE = 10,
    FOUR = 1000,
    FIVE = 10000,
    SIX = 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
    ifp = fopen(filename, "r");
    
    //The first line will contain a single integer n, the total number of 
    //tickets bought. Now we will read in that first line
    fscanf(ifp, "%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(ifp, "%s ", &player[i].last);
//       printf("%s \n", player[i].last);
        fscanf(ifp, "%s ", &player[i].first);
 //       printf("%s \n", player[i].first);
        
        for (j = 1; j <= NUMBERS_PLAYED; j++)
        {  fscanf(ifp, "%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]);
    }
    
    int count = 0;
    int prize;
    
    for (i = 0; i < 6; i++)
    {
        for (j = 0; j < 6; j++)
        {
            for (k = 0; k < 6; k++)
            {
                if (player[i].nums_played[j] == winners[k])
                  count++;
                   {
                }
            }
        } 
    if (count == 3)
       prize = THREE;
    else if (count == 4)
         prize = FOUR;
    else if (count == 5)
         prize = FIVE;
    else if (count == 6)
         prize = SIX;
    else
        prize = NONE;
        
   printf("%s %s matched %d numbers and won $%d \n",player[i].first, player[i].last, count, prize);
} 
   

    //Close the input file
    fclose(ifp);
     
    system("PAUSE");
    return 0;   
}

Post the text file.

5
Llewellyn Mark
1 15 19 26 33 46
Young Brian
17 19 33 34 46 47
Cazalas Jonathan
1 4 9 16 25 36
Siu Max
17 19 34 46 47 48
Balci Murat
5 10 17 19 34 47

I just put it in notepad and saved as "input", type input.txt into command prompt

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

#define NUMBERS_PLAYED 6



enum MATCHED
{
    NONE = 0,
    THREE = 10,
    FOUR = 1000,
    FIVE = 10000,
    SIX = 1000000    
};

struct players
{
    char last[19];
    char first[19];
    int nums_played[6];
    int count;
    MATCHED prize;
};

int main (void)
{
    FILE* ifp;
   char filename[1024];
    int winners[6];
    int i, j;
    int k;
    int ticketsbought = 0;
    struct players* player = 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
    fgets(filename, sizeof(filename), stdin);
    if( filename[strlen(filename)-1] == '\n')
        filename[strlen(filename)-1] = '\0';

    //Open file for reading
    ifp = fopen(filename, "r");
    
    //The first line will contain a single integer n, the total number of 
    //tickets bought. Now we will read in that first line
    fscanf(ifp, "%d ", &ticketsbought); 
   
    player = (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(ifp, "%s ", player[i].last);
        fscanf(ifp, "%s ", player[i].first);
        for (j = 0; j < NUMBERS_PLAYED; j++)
        {  
            fscanf(ifp, "%d ", &player[i].nums_played[j]);
        }           
    }
    //Close the input file
    fclose(ifp);
    
    //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 (i = 0; i < ticketsbought; i++)
    {
        player[i].count = 0;
        for (j = 0; j < 6; j++)
        {
            for (k = 0; k < 6; k++)
            {
                if (player[i].nums_played[j] == winners[k])
                  player[i].count++;
            }
        } 
        if (player[i].count == 3)
            player[i].prize = THREE;
        else if (player[i].count == 4)
            player[i].prize = FOUR;
        else if (player[i].count == 5)
            player[i].prize = FIVE;
        else if (player[i].count == 6)
            player[i].prize = SIX;
        else
            player[i].prize = NONE;
        if( player[i].count > 0)
        {
            printf("%s %s matched %d numbers and won $%d \n",
                player[i].first, player[i].last, player[i].count, player[i].prize);

        }

    }
            
   

     
    system("PAUSE");
    return 0;   
}

thank you very much, even though the above code didnt compile i added a few more things to make it work. Thank you for ur time

It compiled ok with vc++ 2010 express, so any problems you had might have been compiler-dependent.

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.