thanks for reply. I will post the result when I"m done. Thanks very much.
@CommonTater: I've fixed the dynamic memory allowance error with the right pointer. However, I'm facing "syntax error before numeric constant" in line 60. Could you guys please check it out for me. Thanks.
Code:// // PID: k1368475 // Assignment: 1 // Knightlottery Ball // Standard library declaration #include <stdio.h> #include <stdlib.h> // Define numbers player and prize for matched numbers #define Played_number 6 // Stores information for the KnightsBallLottoPlayer typedef struct KnightsBallLottoPlayer { char firstName[20]; char lastName[20]; int numbers[6]; int count; } KBLottoPlayer; enum match { three = 10, four = 1000, give = 10000, six = 1000000 }; int main (void) { FILE * infile; FILE * outfile; infile = fopen ("input.txt", "r"); outfile = fopen ("output.txt", "w"); int i, j; int winners [6]; int k; int tickets = 0; KBLottoPlayer * p; //scan the tickets fscanf(infile, "%d", &tickets); p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer)); for (i = 0; i < tickets; i++) { fscanf(infile, "%s", p[i].lastName); fscanf(infile, "%s", p[i].firstName); for (j = 1; j <= Played_number; j++) { fscanf(infile, "%d", p[i].Played_number[j]); } } 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 < Played_number; i++) { printf("%d ", winners[i]); } for (i = 0; i < j; i++) { for (j = 0; j < Played_number; j++) { for (k = 0; k < Played_number; k++) { if (p[i].Played_number[j] == winners[k]) { p[i].count++; } } } } for (i = 0; i , tickets; i++) { if (p[i].count >= 3) printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count); // close the file, write the file, and free memory free(p); fclose(infile); fclose(outfile); system("PAUSE"); return 0; } }
I'm not at all interested in counting lines... You know the error is on line 59 or 60 ... You know it's syntax ... shouldn't be that hard to figure out.
You can't do 6[j], which is what that line 60 becomes after you substitute the define. Your struct doesn't have a member called Played_number....
sorry CommonTater. This is my first time here, basically i'm just novice for all these. It might not hard for you, but seems abit hard for me though I"m specialize on electrical engineering. This class I have to take in order to graduate for no EE elective in summer. This is what I got so far from the help of you and tabstop and fayyazlodhi from codecall. I got stuck atwith erro fo "numbers" undeclared first use in fucntion. I thought I declare in in typedef struct already.Code:for (j = 0; j < numbers; j++)
this is my new revised code. Appreciate your help.
Code:// PID: k1368475 // Assignment: 1 // Knightlottery Ball // Standard library declaration #include <stdio.h> #include <stdlib.h> // Define numbers player and prize for matched numbers #define Played_number 6 // Stores information for the KnightsBallLottoPlayer typedef struct KnightsBallLottoPlayer { char firstName[20]; char lastName[20]; int numbers[6]; int count; } KBLottoPlayer; enum match { three = 10, four = 1000, give = 10000, six = 1000000 }; int main (void) { FILE * infile; FILE * outfile; infile = fopen ("input.txt", "r"); outfile = fopen ("output.txt", "w"); int i, j; int winners [5]; int k; int tickets = 0; KBLottoPlayer * p; //scan the tickets fscanf(infile, "%d", &tickets); p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer)); p[i].numbers[j]; for (i = 0; i < tickets; i++) { fscanf(infile, "%s", p[i].lastName); fscanf(infile, "%s", p[i].firstName); for (j = 1; j <= Played_number; j++) { fscanf(infile, "%d", p[i].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 < Played_number; i++) { printf("%d ", winners[i]); } for (i = 0; i < j; i++) { for (j = 0; j < numbers; j++) { for (k = 0; k < numbers; k++) { if (p[i].numbers[j] == winners[k]) { p[i].count++; } } } } for (i = 0; i , tickets; i++) { if (p[i].count >= 3) printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count); // close the file, write the file, and free memory free(p); fclose(infile); fclose(outfile); system("PAUSE"); return 0; } }
Last edited by kuro_ng; 05-25-2011 at 03:01 PM.
@tabstop. Appreciate your help tabstop. You give me a good solution for my previous error. I've been revised it so for got that I use the "#define Played_number 6" instead of "numbers 6". I've changed it.
How could you fix the 6[j] ?
You need to use the name of your array, not the number 6. The name of your array is "numbers".
@tabstop: I got it compiled but with blank result o_O !!!!!!! I've a give a wrong structure!!!???
Code:// PID: k1368475 // Assignment: 1 // Knightlottery Ball // Standard library declaration #include <stdio.h> #include <stdlib.h> // Define numbers player and prize for matched numbers #define Played_number 6 // Stores information for the KnightsBallLottoPlayer typedef struct KnightsBallLottoPlayer { char firstName[20]; char lastName[20]; int numbers[6]; int count; } KBLottoPlayer; enum match { three = 10, four = 1000, give = 10000, six = 1000000 }; int main (void) { FILE * infile; FILE * outfile; infile = fopen ("input.txt", "r"); outfile = fopen ("output.txt", "w"); int i, j; int winners [6]; int k; int tickets = 0; KBLottoPlayer * p; //scan the tickets fscanf(infile, "%d", &tickets); p = (KBLottoPlayer *)malloc(tickets*sizeof(KBLottoPlayer)); p[i].numbers[j]; for (i = 0; i < tickets; i++) { fscanf(infile, "%s", p[i].lastName); fscanf(infile, "%s", p[i].firstName); for (j = 1; j <= Played_number; j++) { fscanf(infile, "%d", p[i].numbers[j]); } } 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 < Played_number; i++) { printf("%d ", winners[i]); } for (i = 0; i < j; i++) { for (j = 0; j < p[i].numbers[j]; j++) { for (k = 0; k < p[i].numbers[j]; k++) { if (p[i].numbers[j] == winners[k]) { p[i].count++; } } } } for (i = 0; i , tickets; i++) { if (p[i].count >= 3) printf("%s %s matched %d numbers and won $%d.\n", p[i].firstName, p[i].lastName, p[i].count); // close the file, write the file, and free memory free(p); fclose(infile); fclose(outfile); system("PAUSE"); return 0; } }
fscanf is just like scanf in what it expects you to provide it with for argument types:Your compiler should be complaining to you about that.Code:for (j = 1; j <= Played_number; j++) { fscanf(infile, "%d", p[i].numbers[j]);
Quzah.
Hope is the first step on the road to disappointment.