Hi, still very new to the C Language and just needed some Clearing up and some help please.

Im writing a program where i have to read from a file that says the name of a person and 6 "lottery" numbers in this format.
[ Mike Kobalt
12 15 34 37 45 50]
Then the user inputs the winning numbers and the output is the name and how much they won.
My question is How would we compare the winning numbers with the numbers from the text using parsing and malloc. Heres my code so far...keep in mind im new


#define MAXNUM 56
#define Win3 10
#define Win4 100
#define Win5 10000
#define Win6 1000000

int main() {

int i, size, tokensize *p; //declare variables and pointers
int a, b, c, d, e, f;
int num;

char filename[(2*tokensize)+1][6]

printf("Enter the name of the file with the ticket data\n");
scanf("%s",fileName);

FILE*pFile = fopen(filename,"r"); //open file

if(pFile != NULL) {
fscanf(pFile, "%d", tokensize); //Number of Cards

char *fgets(char*str,int num,FILE*stream); //gets the string and divides into tokens
char *strtok(char*astr, const char*delimeters);

char *p;
p=strtok(NULL, " ");

p=(int *)malloc(size*sizeof(tokensize)); //reads in the numbers of the array and allocates memory of the array

for(i=0; i<6; i++) //Should go through each token
fscanf(fp,%d, &p);

printf("Enter the Winning Lottery Numbers\n");
scanf("%d, %d, %d ,%d, %d, %d\n",str, &a, &b, &c, &d, &e, &f);

if(str == strtok)..

Keep in mind there will be more than 1 person in the input file, like 5 different people with 6 numbers each.

Just a quick note:

1) On programming forums, you NEED to use CODE tags. In the editing window, highlight your code, and click on the

icon.

Otherwise, your code is difficult to study, and will be generally ignored.

2) You don't have any #include files listed in your program.

3) You are using tokensize before you initialize it to anything.

4) You need to include <stdlib.h> for malloc(), and there is no need to cast the pointer it returns - it will be done for you, in C. 

5) I would use a struct containing one name, and six integers. Declare the struct's members above main(). Inside main(), create your array of structs, and you can do all the checking with loops.

6) There is a simple way of doing this, with just one string array[][] and one number array[][]. That might be a lot easier that using structs and etc., as in #5.

I don't see any need to tokenize anything. 

[code]

While((you have data)) {  //use fscanf() or preferrably, fgets()
  get the name
  for(i=0;i<6;i++) {
    get the six numbers 
    and handle them as you wish
  }
}

/* you have all your data, now, proceed
to the next step of your program */

To compare the numbers in each perosn with the winning numbers i would use 2 main steps.

1: Sort the numbers in ascending order.

2: Use a function.
Use a function to compare one set to the other to see if they match

int checkLottoMatches(int* player, int* winning)
{
  int numMatches = 0;

  for(int n = 0; n<6; n++) //assuming there are 6 numbers
  {
     for(int i = 0; i<6; i++)//player has 6 numbers to check against each winning number
     {
        if(*(winning+n)==*(player+i)) numMatches++;
     }
  }
  return numMatches;
}

The code above checks all the players numbers against each of the winning numbers to ensure all matches are found

(say winning numbers are 10,15,20,50.... players numbers are 6,9,15,33,.... then comparing wNum[x] == pNum[x] would not find matches unless by fluke or they got all of them matched )

Hope this helps (bear in mind i typed my code i didnt check it but it should be along the right lines)

Thanks guys it really help

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.